I've created a brand new Project in .NET Core 3.1 and a query I am creating is generating a SQL query (viewed in SQL Server Profiler) that does not contain a where clause and instead reads in the entire table. Is there a way to pass in a predicate and have that incorporated into the SQL statement that is ultimately generated?
In the below function query1
is an IQueryable<Balance>
and query2
is an IEnumerable<Balance>
:
public int GetCount(Func<Balance, bool> predicate)
{
var query1 = (from b in _appContext.Balance
select b
);
var query2 = query1.Where(predicate);
var count = query2.Count();
return count;
}
Called like this:
GetCount(p => p.Email == 'test@gmail.com');
This answer helped me out: Create Expression from Func .
Basically I had to wrap the Func in an Expression as follows:
public int GetCount(Expression<Func<Balance, bool>> predicate)
{
return = (from b in _appContext.TableName
select b
).Where(predicate).Count();
}
The same call worked:
GetCount(p => p.Email == 'test@gmail.com');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.