简体   繁体   中英

Left outer Join with LINQ

Can someone help me translate this query to LINQ? I cant find a good way to translate it, Thanks!

SELECT
  C.id,
  C.id_old,
  C.cus_id,
  C.namefirst,
  C.title,

  CP.id as 'cus_phone_jct.id',
  CP.contact_id,
  CP.phone_id,
  CP.ext,

  P.id as 'cus_phone.id',
  P.phone,
  P.typ_id,
  P.status_id,
  P.donotcontact

FROM cus_contact C
LEFT OUTER JOIN cus_phone_jct CP ON C.id = CP.contact_id  
LEFT OUTER JOIN cus_phone P ON CP.phone_id = P.id
WHERE C.cus_id = 4

Try,

from c in DataContext.cus_contact
join cp in DataContext.cus_phone_jct on c.id equals cp.contact_id into cp2 
  from cp3 in cp2.DefaultIfEmpty()
join p in DataContext.cus_phone on cp3.phone_id equals p.id into p2 
  from p3 in p2.DefaultIfEmpty()
where c.cus_id = 4
select 
  c.id,
  cp3.id
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM