0

I have a linq query that I need to have doing left joins instead of inner joins. All of the examples I see show only 1 left join but when I tried it with 2 i couldn't get it right. So how would this change to use 2 left joins?

ruleSets = (from rs in db.RuleSets
                    join brs in db.BatchRuleSets on rs.ID equals brs.RuleSetID
                    join b in db.Batches on brs.BatchID equals b.id
                    where !clientId.HasValue || b.ClientID == clientId.Value
                    where !batchId.HasValue || brs.BatchID == batchId.Value
                    select new
                    {
                        rs.ID,
                        rs.Description,
                        rs.IsActive,
                        rs.CreatedDate,
                        rs.EffectiveDate,
                        rs.ExpirationDate,
                        BatchName = b.FileName,
                        b.ClientID
                    }).ToList().Select(x => new { 
                        x.ID,
                        x.Description,
                        x.IsActive,
                        x.CreatedDate,
                        x.EffectiveDate,
                        x.ExpirationDate,
                        x.BatchName,
                        ClientName = GetClientName(x.ClientID)});
4
  • im not getting an error i just want all of the records regardless of having a BatchRuleSet or Batch Commented Jun 23, 2011 at 15:18
  • 1
    Why are you selecting, then calling ToList(), then selecting again? First of all the ToList() isn't really doing anything because the second select is turning it back to an IEnumerable<T> and second of all you could do your whole transform in the first select Commented Jun 23, 2011 at 15:36
  • Actually you can't because EF doesn't know how to interpret the GetClientName method. You have to get the data back first before you can call it which is why I do ToList. Commented Jun 23, 2011 at 15:53
  • Ah that makes a bit more sense. Commented Jun 23, 2011 at 16:14

3 Answers 3

1

use left join in linq like this....

join t in Web
on websites.WebsiteID equals t.WebsiteID
into wt1
from wt in wt1.DefaultIfEmpty()

after this wt will be use i where conditions and select statement......

by using this concept you can make a left join query in LINQ.....

Sign up to request clarification or add additional context in comments.

2 Comments

my question was how do you do 2 of them
@rushonerok, erm you do the above twice
1
    ruleSets = (from rs in db.RuleSets
                join brs in db.BatchRuleSets on rs.ID equals brs.RuleSetID into j1

                from jbrs in j1.DefaultIfEmpty()
                join b in db.Batches on jbrs.BatchID equals b.id into j2

                from jb in j2.DefaultIfEmpty()
                where !clientId.HasValue || jb.ClientID == clientId.Value
                where !batchId.HasValue || jbrs.BatchID == batchId.Value
                select new
                {
                    rs.ID,
                    rs.Description,
                    rs.IsActive,
                    rs.CreatedDate,
                    rs.EffectiveDate,
                    rs.ExpirationDate,
                    BatchName = jb.FileName,
                    jb.ClientID
                }).ToList().Select(x => new { 
                    x.ID,
                    x.Description,
                    x.IsActive,
                    x.CreatedDate,
                    x.EffectiveDate,
                    x.ExpirationDate,
                    x.BatchName,
                    ClientName = GetClientName(x.ClientID)});

Comments

1
void Main()
{
    var customers = new List<Customer> {new Customer() { CustomerId = 1}, new Customer() { CustomerId = 2}};
    var orders = new List<Order> {new Order() { OrderId = 1, CustomerId = 1}, new Order() { OrderId = 2, CustomerId = 1}};
    var items = new List<Item> {new Item() { ItemId = 1, OrderId = 1}, new Item() { ItemId = 2, OrderId = 1}, new Item() { ItemId = 3, OrderId = 2}};

    var doubleJoin = from customer in customers
    join order in orders on customer.CustomerId equals order.CustomerId
    into customerOrders
    from co in customerOrders.DefaultIfEmpty()
    where (co != null)
    join item in items on co.OrderId equals item.OrderId
    select new {Customer = customer, Orders = co,  Items = item};

    doubleJoin.Dump();
}

public class Customer
{
    public int CustomerId { get; set; }
}

public class Order
{
    public int OrderId { get; set; }
    public int CustomerId { get; set;}
}

public class Item
{
    public int ItemId { get; set; }
    public int OrderId { get; set; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.