簡體   English   中英

LINQ語句的Where子句中的子查詢

[英]Subquery in Where Clause of LINQ statement

因此,我嘗試按照示例在此LINQ查詢的where子句中使用子查詢。

var innerquery =
    from app in context.applications
    select new { app.app_id };

IEnumerable<postDatedCheque> _entityList = context.postDatedCheques
    .Where(e => innerquery.Contains(e.appSancAdvice.application.app_id));

目的是從postDatedCheques中選擇應用程序表中具有app_id的那些記錄。

但是我在where子句中遇到了錯誤:

  1. 代表'System.Func'不接受1個參數
  2. 無法將lambda表達式轉換為“字符串”類型,因為它不是委托類型
  3. 'System.Linq.IQueryable'不包含'Contains'的定義,最佳擴展方法重載'System.Linq.ParallelEnumerable.Contains(System.Linq.ParallelQuery,TSource)'具有一些無效的參數
  4. 實例參數:無法從“ System.Linq.IQueryable”轉換為“ System.Linq.ParallelQuery”

我的編碼有誤嗎?

我認為簡單的連接就可以完成任務。 它將濾除沒有相對“應用程序”的“支票”:

  var _entitylist = 
    from cheque in context.postDatedCheques
    join app in context.applications on cheque.appSancAdvice.application equals app
    select cheque;

編輯:

使用.Contains(...)解決方案將轉換為SQL IN語句。 這將是非常低效的。 LINQ的join被翻譯成SQL INNER JOIN這是非常有效的,如果你的數據庫架構以及修整(FKS,索引)

關於什么?

IEnumerable<postDatedCheque> _entityList = context.postDatedCheques.Where(
     e => context.applications.Any(
          x => e.appSancAdvice.application.app_id == x.app_id));

如果要使用兩個語句,請將第一個設置為表達式函數。

Expression<Func<string, bool>> innerQuery = 
          x => context.applications.Any(y => y.app_id == x);

IEnumerable<postDatedCheque _entityList = 
  context.postDatedCheques.Where(
    x => innerQuery(x.appSancAdvice.application.app_id));

嘗試以下方法:

var innerquery =
    from app in context.applications
    select new { app.app_id };

IEnumerable<postDatedCheque> _entityList = context.postDatedCheques
    .Where(e => innerquery.Any(a => a.app_id == e.appSansAdvice.application.app_id));

innerquery是一個匿名類型的IQueryable,其中包含一個app_id
Contains(e.appSancAdvice.application.app_id)這行沒有意義,因為e.appSancAdvice.application.app_id與匿名類型不是同一類型。

只需做:

var _entityList = context.postDatedCheques
                         .Where(e => 
                            context.applications
                                   .Select(a => a.app_id)
                                   .Contains(e.appSancAdvice.application.app_id));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM