0

所以我尝试按照这个例子在这个 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));

目标是从应用程序表中具有app_idpostDatedCheckes中选择那些记录。

但是我在 where 子句中遇到了以下错误:

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

我编码不正确是什么?

4

4 回答 4

5

我认为一个简单的加入就可以完成这项工作。它将过滤掉没有相对“应用程序”的“支票”:

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

编辑:

使用 a 的解决方案.Contains(...)将被翻译成 SQLIN语句。这将是非常低效的。如果您的 DB 架构经过精心修剪(FK、索引), Linqjoin会被翻译成 SQL INNER JOIN,这将非常有效

于 2013-04-25T08:02:49.920 回答
4

关于什么?

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));
于 2013-04-25T08:02:55.563 回答
2

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));
于 2013-04-25T08:02:04.430 回答
1

试试这个:

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));
于 2013-04-25T07:56:46.410 回答