【发布时间】:2018-03-07 17:58:01
【问题描述】:
我有以下查询并想转换为 LINQ。
我尝试过 LINQPad 和 Linqer,但在 Visual Studio 中出现错误。
SELECT DISTINCT
g.TXT_Property,
up.TXT_Page
FROM MD g
LEFT JOIN MD ux ON ux.TXT_Page = @sPage
AND ux.TXT_Property = g.TXT_Property
AND ux.TXT_Product = @sProdAll
AND ux.GID_Section IS NULL
LEFT JOIN MD up ON up.TXT_Page = @sPage
AND up.TXT_Property = g.TXT_Property
AND (ISNULL(up.TXT_Product, '') = @sProd or up.TXT_Product = @sProdAlt)
AND up.GID_Section IS NULL
WHERE
(g.GID_Section IS NULL)
AND g.TXT_Page = @sPage
and (ISNULL(g.TXT_Product, '') = @sProd or g.TXT_Product = @sProdAlt or g.TXT_Product = @sProdAll)
ORDER BY TXT_Property, TXT_Product, TXT_Language
这是我尝试过的:
var query2 = from g in list
join ux in list
on g.Property equals ux.Property into g_ux
where ux.Page == sPage
&& ux.Product == sProdAll
&& ux.Section == null
这是另一个尝试。
我提到过 SQL to LINQ - multiple tables left outer join with where clause referring right table
// 另一个查询
var query = (from g in
((from d in list
where ((d.Section == null || d.Section == uSection) && (d.Product == sProd || (sProd == "" && d.Product == null) || (d.Product == sProdAlt) || (d.Product == sProdAll)))
select (new { d.Property })).ToList())
join gx in
((from d in list
where (d.Section == null) && (d.Product == sProdAll)
select (new { d.Property, d.Page, d.Product, d.Language, d.Value, d.Section })).ToList())
on g.Property equals gx.Property into res1
from a1 in res1.DefaultIfEmpty()
join gp in
((from d in list
where (d.Section == null) && (d.Product == sProd || (sProd == "" && d.Product == null) || (d.Product == sProdAlt))
select (new { d.Property, d.Page, d.Product, d.Language, d.Value, d.Section })).ToList())
on g.Property equals gp.Property into res2
from a2 in res2.DefaultIfEmpty()
join ux in
((from d in list
where (d.Section == uSection) && (d.Product == sProdAll)
select (new { d.Property, d.Page, d.Product, d.Language, d.Value, d.Section })).ToList())
on g.Property equals ux.Property into res3
from a3 in res3.DefaultIfEmpty()
join up in
((from d in list
where (d.Section == uSection) && (d.Product == sProd || (sProd == "" && d.Product == null) || (d.Product == sProdAlt))
select (new { d.Property, d.Page, d.Product, d.Language, d.Value, d.Section })).ToList())
on g.Property equals up.Property into res4
from a4 in res4.DefaultIfEmpty()
orderby (new
{
TXT_Property = g.Property,
TXT_Product = Coalesce((Coalesce(a4.Page, "") == "" ? Coalesce(a4.Product, a3.Product) : Coalesce(a4.Product, "SA")), a3.Product, (Coalesce(a2.Page, "") == "" ? Coalesce(a2.Product, a1.Product) : Coalesce(a2.Product, "SA")), a1.Product),
TXT_Language = Coalesce(a4.Language, a3.Language, a2.Language, a1.Language),
})
select (new
{
TXT_Property = g.Property,
TXT_Product = Coalesce((Coalesce(a4.Page, "") == "" ? Coalesce(a4.Product, a3.Product) : Coalesce(a4.Product, "SA")), a3.Product, (Coalesce(a2.Page, "") == "" ? Coalesce(a2.Product, a1.Product) : Coalesce(a2.Product, "SA")), a1.Product),
TXT_Language = Coalesce(a4.Language, a3.Language, a2.Language, a1.Language),
TXT_Value = Coalesce(a4.Value, a3.Value, a2.Value, a1.Value)
}));
【问题讨论】:
-
您可能想要发布一些您尝试过的代码,否则您可能会投下反对票,并且可能无法获得太多帮助。