How is a group in Linq with 2 fields?
3 answers
I believe you want something like this:
var result = from i in info
group i by new { OrderId = i.OrderId, City = i.City } into g
select new { OrderId = g.Key, Infos = g };
Creating a key as an anonymous type simply allows LINQ to use the default equality mappings for all fields of the anonymous type, which should do the job in most situations.
+4
a source to share
Another observation on Noldorin and Josh Einstein's answers ... OrderID will take over the entire key ... which in this case is a new object with two properties: OrderID and City. If your final result set requires OrderID to be the OrderID, you need to do the following:
var result = from i in info
group i by new { i.OrderId, i.City } into g
select new { OrderId = g.Key.OrderId, Infos = g };
+2
a source to share