Skip to content

Instantly share code, notes, and snippets.

@vgebrev
Last active October 15, 2018 08:53
Show Gist options
  • Save vgebrev/0e47919fd47e6ee8d9a3df8494c7a49f to your computer and use it in GitHub Desktop.
Save vgebrev/0e47919fd47e6ee8d9a3df8494c7a49f to your computer and use it in GitHub Desktop.
class DataClass
{
public int Id { get; set; }
public int MyInt { get; set; }
}
var ids = new List<int> { 1, 3 };
var data = new List<DataClass> {
new DataClass { Id = 1, MyInt = 10 },
new DataClass { Id = 1, MyInt = 11 },
new DataClass { Id = 2, MyInt = 12 },
new DataClass { Id = 3, MyInt = 13 },
};
var dictionary = data.GroupBy(d => d.Id)
.Where(g => ids.Contains(g.Key))
.ToDictionary(
k => k.Key,
v => v.Select(d => d.MyInt).ToList()
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment