使用linq左连接2个表

时间:2011-06-23 15:15:39

标签: c# .net linq

我有一个linq查询,我需要做左连接而不是内连接。我看到的所有例子只显示了1个左连接,但是当我用2尝试时,我无法正确使用它。那么如何使用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)});

3 个答案:

答案 0 :(得分:1)

像这样在linq中使用左连接....

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

此wt之后将使用i where条件和select语句......

通过使用这个概念,您可以在LINQ中进行左连接查询.....

答案 1 :(得分: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)});

答案 2 :(得分: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; }
}