0

I need to convert this SQL to LINQ, and would really appreciate some help.

SELECT * FROM Adx_eventSet AS es
LEFT JOIN afx_eventsponsor_eventSet AS spon
ON es.Adx_eventId = spon.adx_eventid

Ive tried this but it's not a left join and so only pulled in the one result.

from t in Adx_eventSet
join x in adx_eventsponsor_eventSet on t.Adx_eventId equals x.adx_eventid
select t
STW
  • 44,917
  • 17
  • 105
  • 161
Phill Healey
  • 3,084
  • 2
  • 33
  • 67
  • 1
    heey I asked more or less the same question today (http://stackoverflow.com/questions/16099057/translate-from-sql-to-linq) I hope you can find some us full informations – Mingebag Apr 19 '13 at 14:30
  • 1
    Another example... http://stackoverflow.com/questions/2742814/left-outer-join-problem – msmucker0527 Apr 19 '13 at 14:31

1 Answers1

1

You want to use DefaultIfEmpty(). See below.

var leftJoin = from adx_event in Adx_eventSet
               join adx_eventsponsor in adx_eventsponsor_eventSet
               on adx_event.Adx_eventId equals adx_eventsponsor.adx_eventid into j 
               from adx_eventsponsor in j.DefaultIfEmpty()
               select new 
               { 
                   Name = adx_event.Name,
                   Name = adx_eventsponsor != null ? adx_eventsponsor.Name : null  
               };

Assuming each table has a Name property.

Sam Leach
  • 12,746
  • 9
  • 45
  • 73