Amount in lambda expression

I am trying to run a query where I get the name of the location and the number of elements in this place. Therefore, if I have a program containing 3 locations, I want to know how many programs are in this place. I need to use this with lambda or linq expression for entities.

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

we just assume that I have a program object with ProgramID, ProgramName, LocationName ... you need to know how many programs are in the location

+3
source share
3 answers

You can do it as follows:

return repository.Count(x => x.Location == "SomeLocation");
+12
source

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());
+2
source

repositoryPattern,

Clients.Where(p => p.DateOfArrival >= DateTime.Now.AddDays(-3) && p.DateOfArrival <= DateTime.Now.AddDays(3)).Select(p => p.ID).Count()

0

Source: https://habr.com/ru/post/1771374/


All Articles