Solved

How to do a Linq left outer join

Posted on 2013-11-26
1
450 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
send messages to whatsapp programatically 2 44
Install Problem 13 29
How to join on ID, with prefix? 15 58
PrintingFoundIt(Almost!(Damn!)) 1 25
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now