开发者

Error iterating over groups in Entity Framework

This code generates errors as indicated:

Error iterating over groups in Entity Framework

Here is the error message:

Error iterating over groups in Entity Framework

The pop-up definition appears to indicate that the fields marked as undefined do exist:

Error iterating over groups in Entity Framework

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜