Solved

How to do a Linq left outer join

Posted on 2013-11-26
1
452 Views
Last Modified: 2016-02-10
I have the following ling join, where I tired to do a left join

    var result = (from dataRows1 in listP
                                  join dataRows2 in listV.Where(d => d.BusinessDate != null)
                                  on new { ID = dataRows1.ID, BusDate = dataRows1.BusinessDate } equals
                                     new { ID = dataRows2.ID, BusDate = dataRows2.BusinessDate }

                                  select new T
                                                  {
                                                    BookPrice =  dataRows1.BookPrice,,
                                                      RateApplied = dateRows2.Rate
                                                  }.DefaultIfEmpty().ToList();

But it stilldoes a join.

I have 8000 rows in LisP, and 7000 rows in ListV, I would expect that I get back 8000 rows in my result
0
Comment
Question by:countrymeister
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39679952
To do a left join in LINQ, you joing into a group, and then select against that group using DefaultIfEmpty.

e.g.

 var result = (from dataRows1 in listP
               join dataRows2 in listV.Where(d => d.BusinessDate != null)
               on new { ID = dataRows1.ID, BusDate = dataRows1.BusinessDate } equals
                  new { ID = dataRows2.ID, BusDate = dataRows2.BusinessDate } into g
               from item in g.DefaultIfEmpty()
               select new T
               {
                   BookPrice =  dataRows1.BookPrice,
                   RateApplied = item == null ? 0 : item.Rate,
               }.DefaultIfEmpty().ToList();

Open in new window

0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JQuery Detecting page post back 6 21
asp.net figure out error 3 26
how to check mis-spellings in a select SQL 2 39
Set arraylist as datatable 2 14
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question