Thursday, September 17, 2015

If you query a bridge table into a simple type and you then want to cast the pairs of ids to a dictionary in C#...

Lambda magic for that is:

List<Bridge> bridges = GetStuff();
Dictionary<int, List<int>> cleanResults = bridges.GroupBy(foo =>
      foo.FooId).ToDictionary(bar => bar.Key, bar => bar.Select(x => x.BarId).ToList());

 
 

Without the .ToDictionary you would just have...

List<Bridge> bridges = GetStuff();
IEnumerable<IGrouping<int,int>> cleanResults = bridges.GroupBy(foo =>
      foo.FooId);

No comments:

Post a Comment