I have 2 tables, Good:
class Good
{
[Key]
public int Id { get; set; }
public int Code { get; set; }
public string Name { get; set; }
public int OrderPoint { get; set; }
public int? GoodGroupId { get; set; }
public virtual GoodGroup GoodGroup { get; set; }
}
and GoodGroup:
class GoodGroup
{
[Key]
public int Id { get; set; }
public int Code { get; set; }
public string Name { get; set; }
}
I want to know count of goods in each GoodGroupName with this code:
var res = db.Goods
.GroupBy(r => r.GoodGroupId)
.Select(m => new
{
m.Name,
m.Count(s => s.Id)
})
.ToList();
But the query doesn't find m.Name; anybody know what my mistake is?
GroupByis a sequence of groups. A group doesn't have a name. It has a key, which in this case would be theGoodGroupId. There's nothing in what you've shown to suggest that everyGoodGroupwith the same ID would have the same name - so what would you expect the "name" of the group to be?GoodGrouptable.GoodGrouIdyou should be able to group onGoodGroupthen in you're selectm.Key.Nameshould be what you want. IfGoodGroupis properly setup.