casterle
casterle

Reputation: 1047

Error iterating over groups in Entity Framework

This code generates errors as indicated:

sample code

Here is the error message:

error message

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

pop-up definition

What am I missing?

(Bonus points for telling me why some of error underlines in the code are red while others are blue.)

Upvotes: 0

Views: 231

Answers (2)

StriplingWarrior
StriplingWarrior

Reputation: 156504

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.

Upvotes: 1

Darren Lewis
Darren Lewis

Reputation: 8488

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());
}

Upvotes: 3

Related Questions