I have those maps in my repository.
public IQueryable<AwType> GetAwTypes() { return from awt in _db.AwTypes select new AwType { Id = awt.Id, Header = awt.Header, Description = awt.Description }; } public IQueryable<Aw> GetAws() { return from aw in _db.Aws select new Aw { Id = aw.Id, Bw = (from bw in GetBws() where bw.Id == aw.Bw select bw ).SingleOrDefault(), AwType = (from awt in GetAwTypes() where awt.Id == awAwType select awt ).SingleOrDefault(), AwAttribute = aw.AwAttribute }; }
In service I want to get count of Bws grouped by AwType as List<KeyValuePair<AwType, int>>. When I call that linq query :
var awGroups = from aw in _repository.GetAws() group aw by aw.AwType into newGroup select newGroup; List<KeyValuePair<AwType, int>> RetGroups = new List<KeyValuePair<AwType, int>>(); foreach (var group in awGroups) { RetGroups.Add(new KeyValuePair<AwType, int>(group.Key, group.Count())); } return RetGroups;
I get an error that is saying I can’t group by on an object I have to group by a scalar value like aw.AwType.Id.
Is there a way to get ‘AwType, int’ pairs in one call?
AwType is a reference type. It would be a bad idea to group on that reference type… Each AwType in that query is a unique reference, so n elements would yield n groups.
Try this: