I have main EF core query that append some AND search Criteria to it via IQueryable.Where(). No issue.
And have some OR search Criteria apply to it, via Expression<Func<T, bool>> orFilter
.
And it's concat with Or via: orFilter = orFilter.Or(p=>p.Prop == foo)
;
And finally it will apply to main query via mainQuery.Where(orFilter) as AND search criteria; No issue.
But one of the OR search Criteria is for another table, need to join on other table and it returns IQueryable< T>. I want to apply it to the orFilter via orFilter = orFilter.Or(joinQuery.Expression as Expression<Func<Product, bool>>);
but it didn't work.
Or is there any equivalent way to implement it?
IQueryable<Product> records = DBContext.Product
.Include(p => p.Item1)
...;
if (filterANDCriteria1.HasValue)
{
records= records.Where(p.xxx == filterANDCriteria1);
}
Expression<Func<Product, bool>> orFilter= null;
if(filterOrCriteriaP1.HasValue)
{
orFilter= orFilter.Or(p=>p.yyy==filterOrCriteriaP1 );
}
if(filterOrCriteriaP2.HasValue)
{
//join another table e.g. Category pseudo code
IQueryable<Product> joinQuery =
records.Join(Category.EntityId == Product.Id and Category.EntityType == EntityType.Product and Category.FieldFoo == filterOrCriteriaP2),
(product,category)=> product;
//Want to append the query to orFilter here but it not work and return null
orFilter = orFilter.Or(joinQuery.Expression as Expression<Func<Product, bool>>); //return null
//It only works for AND query, following code will return the products that match join result and other filter (like filterANDCriteria1)
// records = joinQuery;
}
if(orFilter!=null)
{
//Apply whole orFilter as AND filter to main query
records = records.Where(orFilter);
}
return records.ToList();