This question already has answers here:
LEFT OUTER JOIN in LINQ
(24 answers)
Closed 8 years ago.
I am new to Linq, and I need to convert this query to a left outer join between DocumentStores and Orders, as not all documents are related to an order:
var documents=(from d in _dataContextOrders .DocumentStores
join o in _dataContextOrders.Orders on d.OrderID equals o.ID
join t in _dataContextOrders .DocumentTypes on d.DocumentType equals t.DocTypeID
select new
{
d.ID,
o.PORef ,
t.DocTypeDescription,
d.Name,
d.ContentType
}).ToList();
How do I achieve this?
from a in dataContext.<tableA>
join _b in dataContext.<tableB> on a.id equals _b.aid into _b
from b in _b.DefaultIfEmpty()
select <whatyouwanttoselect>
b will be null, if the join on the ids fails
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can i convert this sql query to linq in entity framework core ?
Select a.Id,a.Url,c.Title,a.ParentId,b.totalSubCats
From [Tbl_Menu] As a
LEFT OUTER JOIN
( Select ParentId, Count(*) As totalSubCats From [Tbl_Menu] Group By ParentId ) As b
On a.Id=b.ParentId
LEFT OUTER JOIN [Tbl_Menu] As c On a.Id = c.Id
ORDER BY a.Id
And we have this columns in Menu_Tbl
Id , Url , Title , Description , ParentId
For Left Join I wrote this query but can`t run
var query = (from m1 in _context.Menu_Tbl
join m2 in _context.Menu_Tbl on m1.Id equals m2.Id
into m3 from m in m3.DefaultIfEmpty() select m1);
I think it would be easier to just make that query a stored procedure and then call the stored procedure using yourReturnedDbSet objectReceived = context.yourReturnedDbSet.FromSqlRaw("EXECUTE StoredProcedureName #exampleParam"). But as for converting it you can pretty much use the same syntax if you have the correct objects. You will also need a list that holds the type you are being returned.
This one should work. Just divide big query by small pieces (subqueries)
var grouped =
from m in ctx.Tbl_Menu
group m by new { m.ParentId } into g
select new
{
g.Key.ParentId,
totalSubCats = g.Count()
};
var query =
from a in ctx.Tbl_Menu
join b in grouped on a.Id equals b.ParentId into gj
from b in gj.DefaultIfEmpty()
join c in ctx.Tbl_Menu on a.Id equals c.Id into ggj
from c in ggj.DefaultIfEmpty()
select new
{
a.Id,
a.Url,
c.Title,
a.ParentId,
b.totalSubCats
};
This question already has an answer here:
Linq to SQL - Group By and Count
(1 answer)
Closed 6 years ago.
I need to show the company name with each number of employees (count of persons).
var result2 = (from c in NoOfEntities.listOfCompanies
join s in NoOfEntities.listOfStores on c.Id equals s.CompanyId
join p in NoOfEntities.listOfPersons on s.Id equals p.StoreId
group c by c.Name into newGroup
select newGroup );
I need to add the number of employees but I don`t have any idea how to add that counter.
var result2 = (from c in NoOfEntities.listOfCompanies
join s in NoOfEntities.listOfStores on c.Id equals s.CompanyId
join p in NoOfEntities.listOfPersons on s.Id equals p.StoreId
group p by c.Name into newGroup
select new
{
CompanyName = newGroup.Key,
NumberOfEmployees = newGroup.Count()
});
This question already has answers here:
Linq to Entity Join table with multiple OR conditions
(3 answers)
Closed 6 years ago.
So what I'm trying to do is :-
SELECT * FROM TableA
JOIN TableB ON TableA.OriginPhoneNumber=TableB.Id OR TableA.DestinationPhoneNumber=TableB.Id
Rather strange query I know! But I'm trying to replicate this in EntityFramework/Linq - looking at all the samples I can see a pretty easy way to do it when the join is using an AND (using anonymous types) but does the same result exist for a OR join?
Just do a cross join with a where clause
var results = from a in db.TableA
from b in db.TableB
where a.OriginPhonenumber == b.Id
|| a.DestinationPhoneNumber == b.Id
select new { A = a, B = b };
It's doubtful that an or in a join condition would be more efficient than this, but it's likely that either would result in the same execution plan. Performance aside it will give the same results.
I used Union
var firstJoin=from tbl in TableA
join c in TableB
on c.OriginPhoneNumber Equals tbl.Id
var secondJoin=from tbl in TableA
join c in TableB
on c.DestinationPhoneNumber Equals tbl.Id
var result=firstJoin.Union(secondJoin)
This question already has answers here:
LINQ - Full Outer Join
(16 answers)
Closed 8 years ago.
I have 3 entities in EntityModel: Customer, CustomerAddress (this is junction entity with Ids and orderOnControl columns and cant be ommited in model) and Address. I need to make linq query to FULL JOIN Customer with Address. This query in SQL is plain:
select e1.[AddressID], e1.City, e3.CustomerID, e3.LastName from [SalesLT].[Address] as e1
full join [SalesLT].[CustomerAddress] as e2 on e1.[AddressID] = e2.[AddressID]
full join [SalesLT].[Customer] as e3 on e2.CustomerID = e3.CustomerID
but I need to write this with linq, I found answers where there are 2 entities with many-to-may relation, but couldn't find any with junction entity, I would appreciate any tips
Give this a shot. This LINQ statement will join Customers to Addresses through the junction table CustomerAddress.
var query = from ca in context.CustomerAddress
join a in context.Address on ca.AddressId equals a.AddressId
join c in context.Customer on ca.CustomerId equals c.CustomerId
select new { a.AddressId, a.City, c.CustomerId, c.LastName };
This question already has answers here:
Left outer join in linq
(4 answers)
Closed 8 years ago.
I have a sql query which has innerjoin and leftouter join's which is very tricky to mr to convert to LINQ.
SELECT project.ID, project.No, project.Name,
APPLN.Id,APPLN.Name,SAR.Name
FROM Phasefact phase WITH (NOLOCK)
INNER JOIN AProject project WITH (NOLOCK) on phase.Id = project.ID
INNER JOIN Application APPLN WITH (NOLOCK) ON project.AppId = APPLN.Id
LEFT OUTER JOIN Master master WITH (NOLOCK) ON phase.amId = master.Id
INNER JOIN Ref SAR WITH (NOLOCK) ON SAR.ID = master.Ref_Id
WHERE phase.ID = 123
It is bit confusing as it contains "left outer join". Someone please help.
Maybe something like this:
var result=(
from phase in db.Phasefact
join project in db.AProject
on phase.Id equals project.ID
join APPLN in db.Application
on project.AppId equals APPLN.Id
//Left join
from master in db.Master
.Where(a=>a.Id==phase.amId).DefaultIfEmpty()
join SAR in db.Ref
on SAR.ID equals master.Ref_Id
where phase.ID == 123
select new
{
project.ID,
project.No,
project.Name,
APPLN.Id,
APPLN.Name,
SAR.Name
}
);
EDIT
But i don't get the left join. Why are you using a left join? To me it looks like you first use a left join:
LEFT OUTER JOIN Master master WITH (NOLOCK) ON phase.amId = master.Id
And the after that you have a join that limits the result like this:
INNER JOIN Ref SAR WITH (NOLOCK) ON SAR.ID = master.Ref_Id
This will be the same as doing this:
INNER JOIN Master master WITH (NOLOCK) ON phase.amId = master.Id
INNER JOIN Ref SAR WITH (NOLOCK) ON SAR.ID = master.Ref_Id
I am giving you an basic idea of left outer join in LINQ
var lines = from p in db.Phasefact
join m in db.Master on m.Id equals
p.amId into p_m
where p.ID == 123
from m in p_m.DefaultIfEmpty()
select new { p.ID };
Now you can convert this much more according to your logic... or Query.......