?
Solved

How to do a Linq left outer join

Posted on 2013-11-26
1
Medium Priority
?
455 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

801 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