2011-06-23 181 views
0

我有一個linq查詢,我需要做左連接而不是內連接。我看到的所有例子都只顯示了1個左連接,但是當我用2嘗試它時,我無法正確識別它。那麼這個更改如何使用2個左連接?使用linq左連接2個表

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)}); 
+1

Whar呃你得到了嗎? –

+0

我沒有得到一個錯誤,我只是想要所有的記錄,無論有沒有BatchRuleSet或批次 –

+1

你爲什麼選擇,然後調用'ToList()',然後再次選擇?首先'ToList()'實際上並沒有做任何事情,因爲第二個選擇將它重新設置爲IEnumerable ,其次你可以在第一個選擇中完成整個變換 –

回答

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)}); 
1

使用左連接在這樣的LINQ ....

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

這個重量後,將用在哪裏的條件和select語句......

通過使用此概念,你可以做一個在左LINQ連接查詢.....

+0

我的問題是你怎麼做2他們 –

+0

@rushonerok,呃,你做了上述兩次 –

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; } 
}