【问题标题】:Oracle query to LINQOracle 查询到 LINQ
【发布时间】:2020-06-19 21:43:00
【问题描述】:

我们正在将数据层从 oracle 转移到实体框架。 我正在尝试在 LINQ 中编写这个 oracle 查询

  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# 中重写它?

【问题讨论】:

  • 到目前为止,您在 LINQ 中尝试过什么?向我们展示您的实体模型和代码。
  • 添加了正在尝试的 linq 代码

标签: oracle entity-framework linq


【解决方案1】:

您可以使用匿名类型来使用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_idld203_ent_id 的数据类型必须相同)

欲了解更多信息,请查看:Create a new anonymous type list to union onto existing anonymous type list

【讨论】:

  • 谢谢塞利姆。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-15
  • 2010-11-12
  • 2016-11-11
相关资源
最近更新 更多