left outer join problem - linq

I need to convert some SQL statement to LINQ. How convert LEFT OUTER JOIN to equivalent LINQ statement?

You need to use the DefaultIfEmpty operator. The below code should result in a left outer join.
var q = from c in customers
join o in orders on c.Key equals o.Key into g
from o in g.DefaultIfEmpty()
select new {Name = c.Name, OrderNumber = o == null ? "(no orders)" : o.OrderNumber};
Credit to: http://www.hookedonlinq.com/OuterJoinSample.ashx

Related

LINQ - Subquery or LEFT OUTER JOIN?

I am trying to optimize my LINQ query performance, and I've noticed a lot of LEFT OUTER JOINs being generated. I know that in SQL, there are some cases in which a single row subquery works better than the equivalent LEFT OUTER JOIN.
For example:
Query 1:
select f.FacilityName, p.Id PatientId, u.DOB, (select u2.NameComputed from adm.Staffs s inner join dbo.AspNetUsers u2 on s.UserId = u2.Id where s.Id = p.StaffId) AssignedTo
from ptn.Patients p
inner join dbo.AspNetUsers u on p.UserId = u.Id
inner join hco.Hcos f on p.HcoId = f.Id
where p.IsRemoved = 0
Query 2:
select f.FacilityName, p.Id PatientId, u.DOB, u2.NameComputed
from ptn.Patients p
inner join dbo.AspNetUsers u on p.UserId = u.Id
inner join hco.Hcos f on p.HcoId = f.Id
left outer join adm.Staffs s on p.StaffId = s.Id
left outer join dbo.AspNetUsers u2 on s.UserId = u2.Id
where p.IsRemoved = 0
Query 1 takes less than a second. Query 2 takes about 45 seconds. If I wanted to make sure LINQ is structured in such a way as to take advantage of this in some cases, how would I go about doing that? That is, is there a way to write a LINQ statement that produces a subquery instead of a LEFT OUTER JOIN?

convert sql to linq with left outer join

I have a sql having left outer join, I want to convert it to linq. the sql is
Select P.Surname, P.Othername H.history
from customers P left outer join historyfiles H on H.patID = P.patId
and H.category1 = 4
where P.id = 2299
Thank you very much.
You can try this:
var query = from p in db.customers.Where(t => t.id == 2299 )
join h in db.historyfiles.Where(l => l.category1 == 4 ) on p.patID equals h.patID into gj
from h in gj.DefaultIfEmpty()
select new { p.Surname, p.Othername, h.History };

Linq left outer join and group by issue

Hi All my following query returns results like this:
lstView.DataSource = (from h in context.HolidayMains
join hd in context.HolidayDetails on h.Id equals hd.HolidayMainId into hd2
from hd in hd2.DefaultIfEmpty()
join e in context.Employees on h.CreatedBy equals e.Id into e2
from e in e2.DefaultIfEmpty()
join o in context.OfficeLocations on hd.OfficeLocation equals o.Id into o2
from o in o2.DefaultIfEmpty()
select new
{
h.HolidayTitle,
h.Date,
OfficeLocation = o.Name,
CreatedBy = e.Name,
h.Id
}).ToList();
But I need result like this :
How it can be done?

How to write multiple outer left join in linq?

I am trying to outer join multiple tables. Here I have pasted my code what I am trying to do.
from o in entities.O
join p in entities.P on o.PID equals p.ID
join dC in entities.C on o.DCID equals dC.ID
join hC in entities.C on o.HCID equals hC.ID
join e in entities.E on pat.AN equals e.UID
join oT in entities.OT on o.OT equals oT.ID
join plt in entities.PLT on p.LT equals plt.ID
join puO in entities.PUO on o.PUTId equals puO.ID into pots
from x in pots.DefaultIfEmpty()
join op in entities.OP on o.OPID equals op.ID
join prt in entities.PatientRelationshipTypes on pat.PRTId equals prt.ID
join secPRT in entities.PRT on pat.SRTId equals secPRT.ID
join patDiagCode in entities.PDiagnosisCodes on pat.ID equals patDiagCode.PID
//from y in pdc.DefaultIfEmpty()
join diagCode in entities.DCodes on patDiagCode.CodeID equals diagCode.ID into dc
from z in dc.DefaultIfEmpty()
where o.ID == orderId
//from x in pots.DefaultIfEmpty()
//from y in pdc.DefaultIfEmpty()
...
...
How do I write correct syntax for multiple outer join in Linq?
Thanks in advance

How to get an outerjoin on a Linq query

Given this linq query
from c in context.Customers
from o in c.Orders
where c.City == "MyCity" || o.ShipTo == "MyCity"
select c
the query will not return any rows if the customer's city is "MyCity" but does not have any orders. This is because of the implied inner join between Customers and Orders. How do I select customers with a City of "MyCity" or order shipped to "MyCity
In this case I need an outer join between Customers and Orders. How do I express that in Linq? I think the approximate TSQL would be
select customers.*
from customers
left join orders on customers.id = orders.customerid
where customers.city = 'MyCity' or orders.ShipTo = 'MyCity'
To get an outer join, you can use DefaultIfEmpty. See this question: Linq to Sql: Multiple left outer joins
from c in context.Customers
from o in context.Orders
.Where(a => a.customerid == c.id)
.DefaultIfEmpty()
where c.City == "MyCity" || o.ShipTo == "MyCity"
select c
Alternatively, you can do this:
from c in context.Customers
join o in context.Orders on c.id equals o.customerid into g
from x in g.DefaultIfEmpty()
where c.City == "MyCity" || x.ShipTo == "MyCity"
select c
I believe they both generate the same SQL.
You have to use DefaultIfEmpty
I think something like this would work
var result = from c in context.Customers
join o in c.Orders on c.CustomerId equals o.CustomerId into lj
from or in lj.DefaultIfEmpty()
where c.City == "MyCity" || or.ShipTo == "MyCity"
select c

Resources