PredicateBuilder where clause issue

452 Views Asked by At

I am using PredicateBuilder to generate where clause

var locationFilter = PredicateBuilder.True<dbCompanyLocation>();
locationFilter = locationFilter.And(s => s.IsPrimary == true && s.State == practiceState);

var companyPredicate = PredicateBuilder.True<dbCompany>();
companyPredicate = companyPredicate.And(c => c.dbCompanyLocations.Where(locationFilter));

I am getting following error, Any one can help for this or am i doing something wrong.

Instance argument: cannot convert from 'System.Data.Linq.EntitySet' to 'System.Linq.IQueryable'

1

There are 1 best solutions below

0
On

The immediate problem seems to be that dbCompany.dbCompanyLocations is an EntitySet, which implements IEnumerable<T> rather than IQueryable<T>. This means its Where extension method expects a Func<dbCompanyLocation, bool>, however the locationFilter variable you are providing is an Expression<Func<dbCompanyLocation, bool>>.

You can create a Func<dbCompanyLocation, bool> from locationFilter by calling the Compile method.

Another problem however is that even if it did type check, c => c.dbCompanyLocations.Where(locationFilter) is not a predicate, since Where returns an IEnumerable<T> instead of a bool. You probably meant to use Any instead of Where i.e.

companyPredicate = companyPredicate.And(c => c.dbCompanyLocations.Any(locationFilter.Compile()));