Error iterating over groups in Entity Framework
This code generates errors as indicated:
Here is the error message:
The pop-up definition appears to indicate that the fields marked as undefined do exist:
What am I missing?
(Bonus points for telling me why some of error underlines in the code开发者_JAVA百科 are red while others are blue.)
You have an IQueryable<IGrouping<string,Anonymous>>
and therefore need to reference by Key.
foreach(var g in groups)
{
Console.WriteLine(g.Key.FirstName.Trim());
}
Remember that these are groups, so you've got two levels:
foreach(var g in groups)
foreach(var item in g)
Console.WriteLine(item);
When you do a "Group By" in LINQ, you end up with a series of actual "groups", each of which has a Key
and a series of values. This allows you to perform aggregate functions like Sum
, Min
or Max
on each grouping. If you just want the results with a similar CountryRegion
to end up together in a flattened collection, you may want to try OrderBy
instead.
精彩评论