簡體   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