LINQ语句的Where子句中的子查询 [英] Subquery in Where Clause of LINQ statement

查看:82
本文介绍了LINQ语句的Where子句中的子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试按照示例在此where子句中进行子查询LINQ查询.

So I tried to follow this example to have a sub-query in the where clause of this LINQ query.

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 中选择在 applications 表中具有 app_id 的那些记录.

The objective was to select those records from postDatedCheques that have app_id in applications table.

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

But I am getting following erros inside the where clause:

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

我的编码有误吗?

推荐答案

我认为一个简单的联接就可以完成这项工作.它将滤除没有相对应用"的支票":

I think a simple join would do the job. It will filter out the 'cheques' that have no relative 'app':

  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 ,如果您的数据库架构经过精心修剪(FK,索引),这将非常有效

Solutions using a .Contains(...) will be translated into a SQL IN statement. Which will be very inefficient. Linq join is translated into SQL INNER JOIN which is very efficient if your DB schema is well trimmed (FKs, index)

这篇关于LINQ语句的Where子句中的子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆