【问题标题】:I need to Convert from SQL Query into LINQ/Lambda Expression我需要从 SQL 查询转换为 LINQ/Lambda 表达式
【发布时间】:2019-11-29 00:31:15
【问题描述】:

我需要将此 sql 语句转换为 LINQ/Lambda 表达式,我无法在网上找到它。有人可以帮忙吗

select  distinct o.ownerid ,a.acctnumber , c.cr_rpt_type_value ,c.cr_req_ts ,d.creditscore from Owner o 
inner join AcctOwner a
on o.ownerid =a.ownerid
left join (
select * from Cr_Rpt_Type x where x.cr_req_ts = (select MAX(cr_req_ts) from 
Cr_Rpt_Type y where 
x.cr_rpt_type_value= y.cr_rpt_type_value)) c on c.cr_rpt_type_value = o.ssn_vault_ref_number
left join Cr_Rpt_Type_dms d on d.Cr_Rpt_Rspn_id_dms = c.Cr_Rpt_Rspn_id

【问题讨论】:

  • 你能提供更多关于你为什么要这样做的信息吗?将所有数据库数据拖到客户端,以便 LINQ 可以对其执行一组低效的循环并丢弃可能大量的数据,这可能会造成巨大的资源浪费。 LINQ 是一把锤子,不是每个问题都应该用锤子解决
  • 我正在使用实体框架(.Net 核心),其中业务需求是我必须在我的 c# 代码中从两个不同的架构中加入 4 个表的情况
  • 您可能会花费很长时间来强制 EF 生成与那里相同的 sql,以便严格遵守您的业务需求。
  • @CaiusJard 我明白了,你能帮我解决这些 sql 语句的 linq

标签: sql linq lambda


【解决方案1】:

您可以使用LINQPadLinquer 等工具将SQL 语句转换为Linq。 这些工具涵盖了多种类型的 SQL 表达式。

但值得注意的是

不是每个 SQL 语句都可以转换为 Linq

【讨论】:

    【解决方案2】:

    我得到了答案

    我知道了,谢谢

    (from a in db.AcctOwner
    join c in (
        (from x in db.Cr_Rpt_Type
        where
          x.Cr_req_ts ==
            (from y in db.Cr_Rpt_Type
            where
              x.Cr_rpt_type_value == y.Cr_rpt_type_value
            select new {
              y.Cr_req_ts
            }).Max(p => p.Cr_req_ts)
        select new {
          x
        })) on new { Cr_rpt_type_value = a.Owner.Ssn_vault_ref_number } equals new { Cr_rpt_type_value = c.x.Cr_rpt_type_value } into c_join
    from c in c_join.DefaultIfEmpty()
    join d in db.Cr_Rpt_Type_dms on new { Cr_Rpt_Rspn_id_dms = c.x.Cr_Rpt_Rspn_id } equals new { Cr_Rpt_Rspn_id_dms = d.Cr_Rpt_Rspn_id_dms } into d_join
    from d in d_join.DefaultIfEmpty()
    where
      a.Acctnumber == Convert.ToString(5675771)
    select new {
      a.Owner.Ownerid,
      a.Acctnumber,
      Cr_rpt_type_value = c.x.Cr_rpt_type_value,
      Cr_req_ts = c.x.Cr_req_ts,
      Creditscore = (int?)d.Creditscore
    }).Distinct()
    

    【讨论】:

      【解决方案3】:
      AcctOwners
         .GroupJoin (
            Cr_Rpt_Types
               .Where (
                  x => 
                        (x.Cr_req_ts == 
                           Cr_Rpt_Types
                              .Where (y => (x.Cr_rpt_type_value == y.Cr_rpt_type_value))
                              .Select (
                                 y => 
                                    new  
                                    {
                                       Cr_req_ts = y.Cr_req_ts
                                    }
                              )
                              .Max (p => p.Cr_req_ts)
                        )
               )
               .Select (
                  x => 
                     new  
                     {
                        x = x
                     }
               ), 
            a => 
               new  
               {
                  Cr_rpt_type_value = a.Owner.Ssn_vault_ref_number
               }, 
            c => 
               new  
               {
                  Cr_rpt_type_value = c.x.Cr_rpt_type_value
               }, 
            (a, c_join) => 
               new  
               {
                  a = a, 
                  c_join = c_join
               }
         )
         .SelectMany (
            temp0 => temp0.c_join.DefaultIfEmpty (), 
            (temp0, c) => 
               new  
               {
                  temp0 = temp0, 
                  c = c
               }
         )
         .GroupJoin (
            Cr_Rpt_Type_dms, 
            temp1 => 
               new  
               {
                  Cr_Rpt_Rspn_id_dms = temp1.c.x.Cr_Rpt_Rspn_id
               }, 
            d => 
               new  
               {
                  Cr_Rpt_Rspn_id_dms = d.Cr_Rpt_Rspn_id_dms
               }, 
            (temp1, d_join) => 
               new  
               {
                  temp1 = temp1, 
                  d_join = d_join
               }
         )
         .SelectMany (
            temp2 => temp2.d_join.DefaultIfEmpty (), 
            (temp2, d) => 
               new  
               {
                  temp2 = temp2, 
                  d = d
               }
         )
         .Where (temp3 => (temp3.temp2.temp1.temp0.a.Acctnumber == Convert.ToString (5675771)))
         .Select (
            temp3 => 
               new  
               {
                  Ownerid = temp3.temp2.temp1.temp0.a.Owner.Ownerid, 
                  Acctnumber = temp3.temp2.temp1.temp0.a.Acctnumber, 
                  Cr_rpt_type_value = temp3.temp2.temp1.c.x.Cr_rpt_type_value, 
                  Cr_req_ts = temp3.temp2.temp1.c.x.Cr_req_ts, 
                  Creditscore = (Int32?)(temp3.d.Creditscore)
               }
         )
         .Distinct ()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        • 2020-10-25
        • 1970-01-01
        相关资源
        最近更新 更多