LINQ语句Where子句中的子查询

Kha*_*Ali 0 c# linq asp.net linq-to-entities entity-framework

所以我试着按照这个例子在这个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));
Run Code Online (Sandbox Code Playgroud)

其目的是选择那些记录postDatedChequesAPP_ID应用程序表.

但是我在where子句中跟踪了错误:

  1. 委托'System.Func'不带1个参数
  2. 无法将lambda表达式转换为类型'string',因为它不是委托类型
  3. 'System.Linq.IQueryable'不包含'Contains'的定义和最佳扩展方法重载'System.Linq.ParallelEnumerable.Contains(System.Linq.ParallelQuery,TSource)'有一些无效的参数
  4. 实例参数:无法从'System.Linq.IQueryable'转换为'System.Linq.ParallelQuery'

我的编码错误是什么?

Ask*_*ein 5

我认为简单的联接可以完成这项工作.它将过滤掉没有相对'app'的'支票':

  var _entitylist = 
    from cheque in context.postDatedCheques
    join app in context.applications on cheque.appSancAdvice.application equals app
    select cheque;
Run Code Online (Sandbox Code Playgroud)

编辑:

使用a的解决方案.Contains(...)将被翻译成SQL IN语句.这将是非常低效的.Linq join被翻译成SQL INNER JOIN,如果您的数据库架构得到很好的修整(FK,索引),这是非常有效的


Bob*_*ale 5

关于什么?

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

如果要使用两个语句,请将第一个语句设置为表达式函数。

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));
Run Code Online (Sandbox Code Playgroud)