Lee
Lee

Reputation: 3969

Dynamic LINQ2Entities query cannot convert IEnumerable to bool

The establishment and organisation lines here give the error that they cannot implicitly convert IEnumerable to bool. What am I missing? I can't use greedy operators until the end of the method. I'm sure it's something simple, yet I can't see it.

IQueryable<User> query = context.Users;

if (usvm.EstablishmentId > 0)
    query = query.Where(x => x.UserEstablishments
        .Where(y => y.UserEstablishmentId == usvm.EstablishmentId));

if (usvm.OrganisationId > 0)
    query = query.Where(x => x.UserEstablishments
        .Where(y => y.Establishment.OrganisationId == usvm.OrganisationId));

if (!String.IsNullOrEmpty(usvm.Forename))
    query = query.Where(x => x.Forename == usvm.Forename);

if (!String.IsNullOrEmpty(usvm.Surname))
    query = query.Where(x => x.Surname == usvm.Surname);

if (usvm.DOB != null)
    query = query.Where(x => x.DOB == usvm.DOB);

List<User> m = query.ToList();
return ToViewModel(m);

Upvotes: 0

Views: 453

Answers (1)

Eugene Podskal
Eugene Podskal

Reputation: 10401

You could use Any method:

if (usvm.EstablishmentId > 0)
    query = query.Where(x => x.UserEstablishments
        .Any(y => y.UserEstablishmentId == usvm.EstablishmentId));

if (usvm.OrganisationId > 0)
    query = query.Where(x => x.UserEstablishments
        .Any(y => y.Establishment.OrganisationId == usvm.OrganisationId));

Upvotes: 4

Related Questions