CodeProg
CodeProg

Reputation: 129

.Net MVC Linq query

In my program i have a database with a table containing persons.

Every person has a collection of clothes, which has a collection of fabrics. Say i want to return the number of persons who has clothes that contain cotton.

I only want to count them once even if the person has more than one clothes that contain cotton.

I tried the following and several other solutions but it didn't quite work out for me:

if ((from p in context.Persons
    from c in p.Clothes
    from f in c.Fabrics
    select f.Name == "Cotton").Count();
{ 

Upvotes: 0

Views: 47

Answers (1)

Sam Axe
Sam Axe

Reputation: 33738

var count = database.People
    .Where(p => p.Clothes.Any(c => c.Fabrics.Any(f => f.Name == "Cotton")))
    .Count();

Select all people where any of the clothes' fabrics are Cotton.

Upvotes: 4

Related Questions