繁体   English   中英

当作为谓词传递时,实体框架 Linq 查询不会在 SQL 服务器中生成 where 子句

[英]Entity Framework Linq query not generating where clause in SQL Server when passed as a predicate

我在 .NET Core 3.1 中创建了一个全新的项目,我正在创建的查询正在生成 SQL 查询(在 SQL 中查看) 有没有办法传入一个谓词并将其合并到最终生成的 SQL 语句中?

在下面的 function query1IQueryable<Balance>query2IEnumerable<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;
    }

像这样调用:

    GetCount(p => p.Email == 'test@gmail.com');

这个答案帮助了我: 从 Func 创建表达式

基本上我必须将 Func 包装在一个表达式中,如下所示:

    public int GetCount(Expression<Func<Balance, bool>> predicate)
    {
        return = (from b in _appContext.TableName
                  select b
                  ).Where(predicate).Count();
    }

同样的电话有效:

    GetCount(p => p.Email == 'test@gmail.com');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM