简体   繁体   中英

Multiple Left OUTER OUTER joins in LINQ to Entities

Using Linq To Entities how would I reproduce the following SQL Query?

SELECT  m.MaterialId, m.MaterialName, m.MaterialTitle, vv.NearestTXDate, c.ChannelName
FROM GB_Material m
LEFT OUTER JOIN WF_VideoVersion vv on vv.MaterialID = m.MaterialID
LEFT OUTER JOIN SP_ScheduleEvent se on se.MaterialName = m.MaterialName
INNER JOIN SP_Schedule s on s.ScheduleID = se.ScheduleID
INNER JOIN GB_Channel c on c.ChannelID = s.ChannelID
WHERE LOWER(m.MaterialName) like '%foo%' OR LOWER(m.MaterialTitle) like '%foo%'  

EDIT: I've excepted an answer to this as the answer produces the exact results that this SQL Query does, but be aware that the original SQL query produces an unwanted Cross Join, which i didn't realise when i wrote it.

(from m in context.GB_Material
join vv in context.WF_VideoVersion  on new {m.MaterialID }
                                               equals new { vv.MaterialID } into vv_join
                                             from vv in vv_join.DefaultIfEmpty()
join se in context.SP_ScheduleEvent  on new {m.MaterialName }
                                               equals new { se.MaterialName } into se_join
                                             from se in se_join.DefaultIfEmpty()
 join s in context.SP_Schedule on new {se.ScheduleID } equals new { s.ScheduleID}
join c in context.GB_Channel on new { s.ChannelID } equals new { c.ChannelID }

                                             where
                                              m.MaterialName.ToLower().Contains("foo") || m.MaterialTitle.ToLower() .Contains("foo")

                                             select new
                                             {
                                                 m.MaterialId, m.MaterialName, m.MaterialTitle, vv.NearestTXDate, c.ChannelName

                                             })

Try this query

var objlist =(from m in Contex.GB_Material
from vv in Contex.WF_VideoVersion.Where(x=>x.MaterialID =m.MaterialID ).DefaultIfEmpty()
from se in Contex.SP_ScheduleEvent.Where(x=>x.MaterialName =m.MaterialName ).DefaultIfEmpty()
from s in Contex.SP_Schedule .Where(x=>x.ScheduleID =se.ScheduleID)
from c in Contex.GB_Channel .Where(x=>x.ChannelID =s.ChannelID )
WHERE m.MaterialName.ToLower().Contains("foo") || m.MaterialTitle.ToLower() .Contains("foo")
select new{  m.MaterialId, m.MaterialName, m.MaterialTitle, vv.NearestTXDate, c.ChannelName}).ToList();

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