1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] Multiple Left Join with Linq and Defaults Values

Discussão em 'Outras Linguagens' iniciado por Stack, Julho 20, 2021.

  1. Stack

    Stack Membro Participativo

    I'm trying to write a query that contains multiple left joins in linq in a c# .netcore 2.2 application. I've tried writing the query in linq but it is not properly retrieving all the rows. Query I'm trying to convert is as follows.

    select ISNULL(b.Id, '00000000-0000-0000-0000-000000000000') as 'Bat Id', p.Id as 'ProductId', p.RP, u.UnitName as 'UnitName', ISNULL(b.QTY,0) as 'BusQty', p.[Name] as 'Product Name'
    from Products p left join Bat b
    ON p.Id = b.ProductId
    left join Units u on p.UOId = u.Id;


    linq I have so far

    var allProducts = (from p in _db.Products
    join s in _db.Bat on p.Id equals s.ProductId into ps
    from s in ps.DefaultIfEmpty()
    join u in _db.Units on p.UOId equals u.Id
    select new
    {
    BatId = s.Id == null ? Guid.NewGuid() : s.Id,
    RP = p.RP,
    BusQty = s.QTY == null ? 0 : s.QTY,
    ProductName = p.Name,
    UnitName = u.UnitName,
    ProductId = p.Id,
    }).ToList();

    Continue reading...

Compartilhe esta Página