tags:

views:

55

answers:

2

I am trying to run a query where I get the name of locations and the number of items in that location. So if i have a program that contains 3 locations I want to know how many programs are in that location..I need to use this with a lambda expression or linq to entities.

return Repository.Find(x => x.Location.Name.Count())...clearly missing something here.

we'll just assume I have a Program entity with ProgramID, ProgramName, LocationName...need to know how many programs are in at a location

+4  A: 

You can do it like this:

return repository.Count(x => x.Location == "SomeLocation");
klausbyskov
A: 

Do you want to know the counts for all locations at once?

var locCounts = Repository.GroupBy(prog => prog.Location.Name).ToLookup(g => g.key, g => g.Count());
Matt Ellen
Matt...thanks for the help, this seems to work. The application throws an exception though and Im struggling to fix the issue. When I changed the return type to ILookup<> I went through my entire application and changed all the appropriate methods to look for and call ILookup. But I am not getting this error – The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error. Im sure this is quite tough for you to have an opinion of the fix..but wanted to pass this along...
gevjen
what were you return types before? it shouldn't be hard to get my above code to give out the right type.
Matt Ellen