?
Solved

How to do a Linq left outer join

Posted on 2013-11-26
1
Medium Priority
?
457 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

649 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