[英]Convert SQL (Left Outer Join's) to LINQ
I am having trouble converting a Oracle Sql Query with multiple LEFT OUTER JOIN
to LINQ. 我在将具有多个LEFT OUTER JOIN
的Oracle SQL查询转换为LINQ时遇到麻烦。 My attempts don't return the expected results. 我的尝试未返回预期的结果。 Could somebody help to convert the SQL Query below to LINQ. 有人可以帮助将下面的SQL查询转换为LINQ。
string currentCulture = Culture.GetCulture();
string query = @"SELECT *
FROM CTGLBL g, CTTGLBL ct, CTLANG lang
WHERE g.sysctglbl = ct.sysctglbl(+) AND
ct.sysctlang = lang.sysctlang (+) AND
NVL(lang.activeflag, 1)= 1 AND
(ISOCODE LIKE '" + currentCulture + "%' OR ISOCODE IS NULL)";
ISOCODE belongs to CTLANG Table. ISOCODE属于CTLANG表。
ps. ps。 I can't use tools like LINQPAD or Linqer. 我不能使用LINQPAD或Linqer之类的工具。
But a better practice for your sql (and here converted to linq) is to use join
to join tables and not the where
: 但是,对于您的sql(此处已转换为linq),更好的做法是使用join
联接表,而不是where
:
string currentCulture = Culture.GetCulture();
var result = from g in CTGLBL
join ct in CTTGLBL on g.sysctglbl equals ct.sysctglbl into ctj
from ct in ctj.DefaultIfEmpty()
join lang in CTLANG on ct.sysctlang equals lang.sysctlang into langj
from lang in langj.DefaultIfEmpty()
where (lang == null ? 1 : (lang.activeflag ?? 1)) == 1 &&
(lang?.ISOCODE.StartsWith(currentCulture) || lang?.ISOCODE == null)
select new { g, ct, lang };
You can also have a "nested select"
for your CTLANG
like this: 您还可以为CTLANG
设置一个"nested select"
,如下所示:
string currentCulture = Culture.GetCulture();
var result = from g in CTGLBL
join ct in CTTGLBL on g.sysctglbl equals ct.sysctglbl into ctj
from ct in ctj.DefaultIfEmpty()
join lang in CTTGLBL.Where(lang => lang.activeflag ?? 1 == 1 &&
(lang.ISOCODE.Contains(currentCulture) ||
lang.ISOCODE == null))
on ct.sysctlang equals lang.sysctlang into langj
from lang in langj.DefaultIfEmpty()
select new { g, ct, lang };
(What I see is a left join, not right) (我看到的是左联接,而不是右联接)
Assuming you have the proper relations between the tables in your schema, with SQL server (Linq TO SQL) this would work, not sure if it would supported for Oracle: 假设您的架构中的表之间具有适当的关系,并且使用SQL Server(Linq TO SQL),则可以正常工作,不确定是否支持Oracle:
string currentCulture = Culture.GetCulture();
var data = from g in db.CTGLBL
from ct in g.CTTGLBL.DefaultIfEmpty()
from lang in g.CTLANG.DefaultIfEmpty()
where !g.CTLANG.Any() ||
( lang.activeflag == 1 &&
lang.ISOCODE.StartsWith(currentCulture))
select new {g, ct, lang};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.