I used EF in my .NET 6 Project. I want to do some condition then run query. For Ex:
var ProductModelList = Product.Include(b => b.Brand);
if (BrandId > 0)
{
ProductModelList.Where(b => b.BrandId == BrandId);
}
ProductModelList.Skip(skip).Take(PageSize).ToList();
Is this true way? Thank u to help
CodePudding user response:
Yes it is right way, but your code has issue:
IQueryable
is immutable and you have to assign new query to variable.
var query = Product.Include(b => b.Brand).AsQueryable();
if (BrandId > 0)
{
query = query.Where(b => b.BrandId == BrandId);
}
var ProductModelList = query.Skip(skip).Take(PageSize).ToList();