我们正在将数据层从预言机迁移到实体框架。 我尝试在 LINQ 中编写此预言机查询
SELECT *
FROM (SELECT ld203_entity_id, ld203_entity_nm
FROM f.ld203_ent_lst
WHERE ld203_ent_nm <> 'Other' and LD203_ENT_HIDE='Y'
ORDER BY ld203_ent_nm ASC)
UNION ALL
SELECT *
FROM (SELECT ld203_ent_id, ld203_ent_nm
FROM f.ld203_ent_list
WHERE ld203_ent_nm = 'Other' and LD203_ENT_HIDE='Y'
ORDER BY ld203_ent_nm ASC);
LINQ 查询正在尝试..
var ls =
(from a in context.ld203_entity_list
where (a.ld203_entity_nm != "Other" && a.ld203_entity_hide == "Y")
select a.ld203_entity_id, a.ld203_entity_nm)
.Union
(from b in context.ld203_entity_list
where b.ld203_entity_nm == "Other" && b.ld203_entity_hide == "Y"
select b.ld203_entity_id, b.ld203_entity_nm);
dt = LINQResultToDataTable(ls);
任何人都可以帮助在 LINQ C# 中重写它的方法吗?
您可以使用匿名类型来使用Union
,如下所示:
var ls = (from a in context.ld203_entity_list
where (a.ld203_entity_nm != "Other" && a.ld203_entity_hide == "Y")
select new { Id = a.ld203_entity_id, Num = a.ld203_entity_nm } )
.Union
(from b in context.ld203_entity_list
where b.ld203_entity_nm == "Other" && b.ld203_entity_hide == "Y"
select new { Id = b.ld203_ent_id, Num = b.ld203_ent_nm});
请注意,所有属性都是在所有实例中声明的,并且它们必须具有相同的数据类型。(例如,在您的情况下,这些ld203_entity_id
和ld203_ent_id
的数据类型必须相同(
有关详细信息,请查看:创建新的匿名类型列表以合并到现有匿名类型列表