none
Need left out join using LINQ RRS feed

  • Question

  •  

    Hi ,

    i want to achive some operation using Left out join, any one can help me,

     

    below is example my left table should be sdgetable , i want all data from sdgetable and match with objobsdata with column stationid or observationtime

     

    var Found = from obstable in objobsdata

     

    join sdgetable in objsdgedata on obstable.Observation_Time equals sdgetable.ObservationDateTime

     

    select new { obstable.Station_ID, obstable.Outdoor_Temperature, sdgetable.AWSStation_ID, sdgetable.Temp, sdgetable.ObservationDateTime };

    Wednesday, August 25, 2010 4:01 PM

Answers

  • I got this example from this post

    http://smehrozalam.wordpress.com/2009/06/10/c-left-outer-joins-with-linq/

    Suppose you have this SQL query

     

    Select TL.EntryDate, TL.Hours, J.JobName
    
    From TimeSheetLines TL
    
    Left Join Jobs J on TL.JobNo=J.JobNo

     


    When using inner join you do it like this in LINQ

    var lines =
        from tl in db.TimeSheetLines
        join j  in db.Jobs on tl.JobNo equals j.JobNo
        where tl.ResourceNo == resourceNo
     
        select new
        {
            EntryDate = tl.EntryDate,
            Hours = tl.Hours,
            Job = j.JobName
        };
    


    When using left outer join you do it like this in LINQ

    var lines =
        from tl in db.TimeSheetLines
        join j  in db.Jobs on tl.JobNo equals j.JobNo into tl_j
        where tl.ResourceNo == resourceNo
     
        from j in tl_j.DefaultIfEmpty()
        select new
        {
            EntryDate = tl.EntryDate,
            Hours = tl.Hours,
            Job = j.JobName
        };


    Notice that the only difference is the use of “into” with the join statement followed by reselecting the result using “DefaultIfEmpty()” expression.


    Ali Hamdar (alihamdar.com)
    • Marked as answer by Jumpingboy Monday, August 30, 2010 2:11 PM
    Sunday, August 29, 2010 1:04 PM

All replies