Dylan Gomes
Dylan Gomes

Reputation: 93

Linq statement with 2 joins a where and need a count

This is what i have:

var count = (from p in _db.Personen
                        join pc in _db.Postcodes on p.Postcode equals pc.postcode
                        join r in _db.Regios on pc.RegioId equals r.RegioId
                        where (p.Leeftijd >= leeftijdgetal[0] && leeftijd[1] <= p.Leeftijd) &&
                        r.RegioNaam == regio && p.Geslacht == geslacht
                        select new
                        {
                            teller = Where(p => p.Showit == 1).Count()
                        }).Distinct();

It gives an error on teller. How can i recieve a number of all the persons between 2 ages with that specifiek region and a specifiek gender.

the tables are as followed:

Personen:

-PersoonId

-Naam -Voornaam -Leeftijd -Geslacht -Adres -Postcode -Telefoon -Email -Wachtwoord -RollId -VragenlijstId -Status -MantelverzorgerId -DokterId -eID

Postcodes:

-Postcode -Gemeente -RegioId -PostcodeId

Regios:

-RegioId -RegioNaam

Upvotes: 2

Views: 62

Answers (1)

James
James

Reputation: 82096

It seems to me like you don't need a projection at all here

var count = (from p in _db.Personen
             join pc in _db.Postcodes on p.Postcode equals pc.postcode
             join r in _db.Regios on pc.RegioId equals r.RegioId
             where p.Leeftijd >= leeftijdgetal[0] && leeftijd[1] <= p.Leeftijd && 
                   r.RegioNaam == regio && 
                   p.Geslacht == geslacht &&
                   p.Showit == 1
            )
            .Distinct()
            .Count();

Upvotes: 1

Related Questions