我需要从SQL查询转换为LINQ / Lambda表达式

时间:2019-07-20 05:51:40

标签: sql linq lambda

我需要将此sql语句转换为LINQ / Lambda Expression,但我无法在网上找到它。任何人都可以帮忙

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

3 个答案:

答案 0 :(得分:0)

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

但值得注意的是

  

并非每个SQL语句都可以转换为Linq

答案 1 :(得分:0)

我得到了答案

我得到了答案,谢谢

(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()

答案 2 :(得分:0)

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 ()