[英]Subquery in LINQ that's in the select statement, not the where clause
[英]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子句中遇到了錯誤:
我的編碼有誤嗎?
我認為簡單的連接就可以完成任務。 它將濾除沒有相對“應用程序”的“支票”:
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.