I'm trying to join 1 table onto 2 other tables with LINQ, but I can't seem to figure out how this is done.
I can make it work writing pure SQL statements within Visual studio, I'm just not sure how to convert this into LINQ.
Here's my SQL statement:
SELECT c.CustomerId, c.CustomerName, pw.Number, pc.Number FROM Customers as c
LEFT JOIN Tasks as k ON k.Id = c.Task_Id
LEFT JOIN Workers as w ON w.Id = k.Worker_Id
LEFT JOIN PersonNumbers as pw ON pw.Person_Id = w.Id
LEFT JOIN Chiefs as ch ON ch.Id = k.Chief_Id
LEFT JOIN PersonNumbers as pc ON pc.Person_Id = ch.Id
Perhaps this requires a bit of explanation.
We have a bunch of Customers and these customers can have some tasks. Within a task you will have workers and chiefs. Within the PersonNumbers table, I have some extra information about workers and chiefs, and this is the information that I need.
You should be able to do something like the following assuming all your joins are based on foreign keys that should result in navigation properties in your entities. The DefaultIfEmpty is what makes everything a left join.
var results = from c in db.Customers
from k in c.Tasks.DefaultIfEmpty()
from w in k.Workers.DefaultIfEmpty()
from pw in w.Persons.DefaultIfEmpty()
from ch in k.Chiefs.DefaultIfEmpty()
from pc in ch.Persons.DefaultIfEmpty()
select new
{
c.CustomerId,
c.CustomerName,
pw.Number,
pc.Number
};
If you don't have the navigation properties then you'll have to use join.
var results = from c in db.Customers
join xk in db.Tasks on xk.Id equals c.Task_Id
from k in xk.DefaultIfEmpty()
join xw in db.Workers on xw.Id equals k.Worker_Id
from w in xw.DefaultIfEmpty()
join xpw in db.Persons on xpw.Person_Id equals w.Id
from pw in xpw.DefaultIfEmpty()
join xch in db.Chiefs on xch.Id equals k.Chief_Id
from ch in xch.DefaultIfEmpty()
join xpc in db.Persons on xpc.Person_Id euals ch.Id
from pc in xpc.DefaultIfEmpty()
select new
{
c.CustomerId,
c.CustomerName,
pw.Number,
pc.Number
};
Related
So I have a SQL query that I would like to convert to LINQ.
Here is said query:
SELECT *
FROM DatabaseA.SchemaA.TableA ta
LEFT OUTER JOIN DatabaseA.SchemaA.TableB tb
ON tb.ShipId = ta.ShipId
INNER JOIN DatabaseA.SchemaA.TableC tc
ON tc.PostageId= tb.PostageId
WHERE tc.PostageCode = 'Package'
AND ta.MailId = 'Specification'
The problem I am struggling with is I cannot seem to figure out how to do a left join in LINQ before an inner join, since doing a left join in LINQ is not as clear to me at least.
I have found numerous examples of a LINQ inner join and then a left join, but not left join and then inner join.
If it helps, here is the LINQ query I have been playing around with:
var query = from m in tableA
join s in tableB on m.ShipId equals s.ShipId into queryDetails
from qd in queryDetails.DefaultIfEmpty()
join p in tableC on qd.PostageId equals p.PostageId
where m.MailId == "Specification" && p.PostageCode == "Package"
select m.MailId;
I have tried this a few different ways but I keep getting an "Object reference not set to an instance of an object" error on qd.PostageId.
LINQ is very new to me and I love learning it, so any help on this would be much appreciated. Thanks!
From my SQL conversion recipe:
JOIN conditions that aren't all equality tests with AND must be handled using where clauses outside the join, or with cross product (from ... from ...) and then where
JOIN conditions that are multiple ANDed equality tests between the two tables should be translated into anonymous objects
LEFT JOIN is simulated by using into joinvariable and doing another from from the joinvariable followed by .DefaultIfEmpty().
The order of JOIN clauses doesn't change how you translate them:
var ans = from ta in TableA
join tb in TableB on ta.ShipId equals tb.ShipId into tbj
from tb in tbj.DefaultIfEmpty()
join tc in TableC on tb.PostageId equals tc.PostageId
where tc.PostageCode == "Package" && ta.MailId == "Specification"
select new { ta, tb, tc };
However, because the LEFT JOIN is executed before the INNER JOIN and then the NULL PostageIds in TableB for unmatched rows will never match any row in TableC, it becomes equivalent to an INNER JOIN as well, which translates as:
var ans2 = from ta in tableA
join tb in tableB on ta.ShipId equals tb.ShipId
join tc in tableC on tb.PostageId equals tc.PostageId
where tc.PostageCode == "Package" && ta.MailId == "Specification"
select new { ta, tb, tc };
Use:
var query = from m in tableA
join s in tableB on m.ShipId equals s.ShipId
join p in tableC on s.PostageId equals p.PostageId
where m.MailId == "Specification" && p.PostageCode == "Package"
select m.MailId;
Your query uses a LEFT OUTER JOIN but it doesn't need it.
It will, in practice, function as an INNER JOIN due to your tc.PostageCode = 'Package' clause. If you compare to a column value in a table in a WHERE clause (and there are no OR clauses and you aren't comparing to NULL) then effectively all joins to get to that table will be treated as INNER).
That clause will never be true if TableB is null (which is why you use LEFT OUTER JOIN vs INNER JOIN) - so you should just use an INNER JOIN to make the problem simpler.
How to convert store procedure to linq in nopCommerce c#
My store procedure query
SELECT p.Id
FROM Product p WITH (NOLOCK)
LEFT JOIN Discount_AppliedToProducts dap WITH (NOLOCK)
ON p.Id = dap.Product_Id
LEFT JOIN Product_Category_Mapping pcm WITH (NOLOCK)
ON p.Id = pcm.ProductId
LEFT JOIN Discount_AppliedToCategories dac WITH (NOLOCK)
ON pcm.CategoryId = dac.Category_Id
AND dac.Category_Id IN (1 ,2 ,3 ,4 ,5 ,6)
LEFT JOIN Product_Manufacturer_Mapping pmm WITH (NOLOCK)
ON p.Id = pmm.ProductId
LEFT JOIN Discount_AppliedToManufacturers dam WITH (NOLOCK)
ON pmm.ManufacturerId = dam.Manufacturer_Id
WHERE dap.Discount_Id IN (3)
OR dac.Discount_Id IN (3)
OR dam.Discount_Id IN (3)
My linq query
var productlist = (from q in _productRepository.Table
select q).ToList();
var discount_AppliedToProductIds = (from dp in _discountRepository.Table
from p in dp.AppliedToProducts
select p).ToList().DistinctBy(d => d.Id).ToList();
var discount_AppliedToCategorieIds = (from dp in _discountRepository.Table
from c in dp.AppliedToCategories
select c).ToList().DistinctBy(d => d.Id).ToList();
var discount_AppliedToManufacturerIds = (from dp in _discountRepository.Table
from m in dp.AppliedToManufacturers
select m).ToList().DistinctBy(d => d.Id).ToList();
var product_Manufacturer_Mapping = (from dp in productlist
from pm in dp.ProductManufacturers
select pm).ToList().DistinctBy(d => d.Id).ToList();
var product_Category_Mapping = (from dp in productlist
from pc in dp.ProductCategories
select pc).ToList().DistinctBy(d => d.Id).ToList();
var ss = (from p in productlist
join dap in discount_AppliedToProductIds on p.Id equals dap.Id
join pcm in product_Category_Mapping on p.Id equals pcm.ProductId
//join dac in discount_AppliedToCategorieIds on pcm.CategoryId equals dac.Id
from dac in discount_AppliedToCategorieIds
join pmm in product_Manufacturer_Mapping on p.Id equals pmm.ProductId
join dam in discount_AppliedToManufacturerIds on pmm.ManufacturerId equals dam.Id
from dapd in dap.AppliedDiscounts
from pacd in dac.AppliedDiscounts
from damd in dam.AppliedDiscounts
where discountIds.Any(d => dapd.Id == d || d == pacd.Id || d == damd.Id)
// innner join condition
where categoryIds.Any(d => d == dac.Id) && dac.Id == pcm.CategoryId
select p).ToList();
I have write this code into c# but this code not provide proper result. Now I don't know what is problem into this code. If I run this code into sql server, then I get proper result, but in c# code I don't get proper result.
It's been a long time since I wrote a query in LINQ, but I seem to recall that if you wish to model a LEFT JOIN, you have to use DefaultIfEmpty(), otherwise you end up with an INNER JOIN.
See this, it's answer shows where to apply DefaultIfEmpty:
Linq join iquery, how to use defaultifempty
Obviously if you don't correctly model a LEFT JOIN expression, you'll end up with results only when all 3 inputs produce values.
I would also suggest not using .ToList() on each of your source queries, because that's going to manifest data into memory and use LINQ to Objects for your final query. If you remove the .ToList(), LINQ will construct a single database query for the entire process.
Mark
Let's say I have this query in SQL:
SELECT
....
FROM
TableA a
JOIN TableB b on a.Id = b.Id
I've got that working just fine as a linq query:
var results = (from a in db.TableA
join b in db.TableB on a.Id equals b.Id
select new MyObject {...}).ToList();
But now I want to add a left join to the mix as well. In SQL:
SELECT
....
FROM
TableA a
JOIN TableB b on a.Id = b.Id
LEFT JOIN TableC c on a.Id2 = c.Id2
I'm not sure how to handle this in the linq query.
SQL's LEFT JOIN concept is a pretty clunky abstraction when you think about what you're really trying to do. You really want, for every TableA entry, to have a list of TableC items associated with it, right?
LINQ uses a much simpler (IMO) way to represent this abstraction, because it has the concept of nested, hierarchical objects. The provider will generate a LEFT JOIN, but it'll also build the result into an object structure that's easy to work with.
var results = (from a in db.TableA
join b in db.TableB on a.Id equals b.Id
select new MyObject {
...
TableCEntries =
from c in db.TableC
where c.Id == a.Id
select c // or select specific values from c
}).ToList();
If you really want your results flattened out the way they would be with a left outer join, you could do this:
var results = (from a in db.TableA
join b in db.TableB on a.Id equals b.Id
from c in db.TableC
where c.Id == a.Id
select new MyObject {
...
}).ToList();
... but you wouldn't get any entries for TableA where there are no entries present for TableC. For that behavior you'd have to do ugly stuff with group by/DefaultIfEmpty as shown in Ivan Stoev's link.
Before this gets called a duplicate, I have looked around on SO and found a way to do this but it isn't working.
My query is
var GetAllProjects = from f in dc.vw_gmi_all_projects
join mc in dc.gmi_maintenance_classes on f.maintenance_classID equals mc.maintenance_classID
join ms in dc.gmi_maintenance_subclasses on f.maintenance_subclassID equals ms.maintenance_subclassID
join pm in dc.master_project_milestones on f.pmID equals pm.pmID
join ac in dc.vw_master_Countries on f.country_display_name equals ac.country_display_name
join pd in dc.gmi_project_details on f.project_dataID equals pd.project_dataID
join md in dc.vw2_master_districts on f.country_display_name equals md.element_display_name
join ml in dc.vw2_master_lmus on pd.dataID equals ml.elementID into gl from sub in gl.DefaultIfEmpty()
where (mc.maintenance_classID == 3 && ms.maintenance_subclassID != 11)
select new
{
f.project_dataID,
f.projectID,
f.project_title,
f.local_projectID,
f.pm_display_name,
f.reu_name,
f.reuID,
f.sectorID,
f.sector_display_name,
f.country_display_name,
f.maintenance_classID,
f.maintenance_subclassID,
mc.maintenance_class_display_name,
ms.maintenance_subclass_display_name,
pm.pm_name,
ac.region_display_name,
pd.dataID,
md.element_display_name,
ac.cluster_display_name,
display_name = sub.element_display_name
};
foreach (var a in GetAllProjects)
{
lst.Add(new ReportFilter
{
project_dataID = (int)a.project_dataID,
projectID = a.projectID,
project_title = a.project_title,
local_projectID = a.local_projectID,
pm_display_name = a.pm_display_name,
reu_name = a.reu_name,
reuID = a.reuID,
country_display_name = a.country_display_name,
sectorID = a.sectorID,
sector_display_name = a.sector_display_name,
maintenance_classID = a.maintenance_classID,
maintenance_subclassID = a.maintenance_subclassID,
maintenance_class_display_name = a.maintenance_class_display_name,
maintenance_subclass_display = a.maintenance_subclass_display_name,
pm_name = a.pm_name,
region_display_name = a.region_display_name,
dataID = a.dataID,
district = a.element_display_name,
cluster_display_name = a.cluster_display_name
});
}
This is where the left join is supposed to take place..
join ml in dc.vw2_master_lmus on pd.dataID equals ml.elementID into gl from sub in gl.DefaultIfEmpty()
This query runs fine if I leave out the attempt at left join and leave that join out entirely, but I need to get the left join to work so I can get the rest of the records. A typical join won't work like the others because it doesn't return any records.
So where am I going wrong with my query, or doing wrong? I know where its going wrong just not sure how to fix it.
Thanks
More Details
This query creates a list and then I query against this list. However, this query won't return any records because of my attempt at creating a left join.
EDIT
Here is the SQL that I wrote and trying to recreate it using Linq
select * from [vw_gmi_all_projects] f
inner join [gmi_maintenance_classes] mc on f.maintenance_classID = mc.maintenance_classID
inner join [gmi_maintenance_subclasses] ms on f.maintenance_subclassID = ms.maintenance_subclassID
inner join [master_project_milestones] pm on f.pmID = pm.pmID
inner join [vw_master_Countries] ac on f.country_display_name = ac.country_display_name
inner join [gmi_project_details] pd on f.project_dataID = pd.project_dataID
inner join [vw2_master_district] md on f.country_display_name = md.element_display_name
left join [vw2_master_lmu] ml on pd.dataID = ml.elementID
where (mc.maintenance_classID = 3 and ms.maintenance_subclassID != 11)
I hope this helps.
Try the syntax from this answer instead: https://stackoverflow.com/a/4739738/1869660
I think that's an easier syntax when creating LEFT JOINs (note: from, not join).
var GetAllProjects = from f in dc.vw_gmi_all_projects
join mc in dc...
join ms in dc...
join pm in dc...
join ac in dc...
join pd in dc...
join md in dc...
from ml in dc.vw2_master_lmus.Where(ml => ml.elementID == pd.dataID).DefaultIfEmpty()
where ...
I'm struggling with a Linq query involving left joins. Here is the SQL I'm trying to convert:
SELECT EmailStats.EmailAddress, EmailVoting.DateAdded, EmailVoting.ResponseText,
EmailStats.DateSent, EmailAlerts.Name, UserDetails.EmployeeNumber
FROM EmailAlerts
INNER JOIN EmailStats
ON EmailAlerts.EmailAlertID = EmailStats.EmailAlertID
INNER JOIN UserDetails
ON EmailStats.UserId = UserDetails.UserId
LEFT OUTER JOIN EmailVoting
ON EmailAlerts.EmailAlertID = EmailVoting.EmailAlertID
AND EmailVoting.UserId = EmailStats.UserId
Where EmailAlerts.EmailAlertID = 43 AND EmailStats.IsTestSend = 0
The SQL is returning the correct data with the EmailVoting fields null if the rows don't exist. Below is the LINQ I currently have:
from ea in db.EmailAlerts
join es in db.EmailStats on ea.EmailAlertID equals es.EmailAlertID
join ud in db.UserDetails on es.UserId equals ud.UserId
join ev in db.EmailVoting on ea.EmailAlertID equals ev.EmailAlertID into vm
from v in vm.DefaultIfEmpty()
join ev in db.EmailVoting on es.UserId equals ev.UserId into udItems
from u in udItems.DefaultIfEmpty()
where v.EmailAlertID == emailAlertID
What I thought was the same in LINQ, isn't displaying correctly and is in fact displaying an entry with a different EmailAlertID. Anyone see where I might be going wrong?
Thanks
Try this:
from ea in db.EmailAlerts
join es in db.EmailStats on ea.EmailAlertID equals es.EmailAlertID
join ud in db.UserDetails on es.UserId equals ud.UserId
join ev in db.EmailVoting on new {ev.EmailAlertID, ev.UserId} equals new {ea.EmailAlertID, es.UserId} into vm
from v in vm.DefaultIfEmpty()
where v.EmailAlertID == emailAlertID