开发者

left join 2 tables using linq

I have a linq query that I need to have doing left joins inste开发者_如何学编程ad 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)});


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.....


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜