Multiple joins using linq - c#

This is my query:
var results = from table1 in dtSplitDates.AsEnumerable()
join table2 in dtSplitDates.AsEnumerable() on (int)table1["FID"] equals (int)table2["FID"] into lj
from r in lj.DefaultIfEmpty()
select dtSplitDates2.LoadDataRow(new object[]
{
r["FID"],
r["SLNO"],
r == null ? string.Empty : r["Dates"]
}, false);
Currently i am joining on Column FID - due to which i am getting 36 records (duplicates):
However in order to avoid duplicates i need to join also on SLNO column but i am unable to write that query - please help.

As per my understanding you want two join condition; Try this
var results = from table1 in dtSplitDates.AsEnumerable()
join table2 in dtSplitDates.AsEnumerable()
on new {id1 =(int)table1["FID"], SLno1= (int)table1["SLNO"]}
equals new {id2=(int)table2["FID"], SLno2=(int)table2["SLNO"]} into lj
from r in lj.DefaultIfEmpty()
select dtSplitDates2.LoadDataRow(new object[]
{
r["FID"],
r["SLNO"],
r == null ? string.Empty : r["Dates"]
}, false);

Try to implement with this example:
For Multiple Joins:
var result=(from com in db.Company.AsEnumerable()
join c in db.Country.AsEnumerable() on com.CountryID equals c.CountryID
join s in db.State.AsEnumerable() on com.StateID equals s.StateID
join ct in db.City.AsEnumerable() on com.CityID equals ct.CityID
orderby com.Name
select new CompanyModel()
{
CompanyID = com.CompanyID,
Name = com.Name,
AddressLine1 = com.AddressLine1,
CountryID = com.CountryID,
StateID = com.StateID,
CityID = com.CityID,
Country = c.CountryID,
State = s.StateID,
City = ct.CityID,
Pin = com.Pin,
Phone = com.Phone,
}).Distinct().ToList();

Related

Multiple Left Join with Linq and Defaults Values

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();
You are missing DefaultIfEmpty() on Units, thereby turning it into an inner join
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 into us
from u in us.DefaultIfEmpty()
select new
{
BatId = s.Id ?? Guid.NewGuid(),
RP = p.RP,
BusQty = s.QTY ?? 0,
ProductName = p.Name,
UnitName = u.UnitName,
ProductId = p.Id,
}).ToList();

Linq join - group by - OrderByDescending query

I have this query:
from i in dc.TFFiles
where i.Tid == tid
group i by new { i.Title, i.Type } into gr
select gr.OrderByDescending(g => g.Version).First();
I want to add a join with another table so I will have access to a field of the second table
Here is the join I need without the group by:
from i in dc.TFFiles
join c in dc.FCategory on i.CatId equals c.id
....
How I can combine the group by query with the join
Tried like this but it doesn't order the results by Version descending. I always get Version 1 even though there are files with higher versions.
var result =
from i in dc.TFFiles
where i.Tid == tid
group i by new { i.Title, i.Type } into gr
orderby gr.FirstOrDefault().Version descending
join c in dc.FCategories on gr.FirstOrDefault().CatId equals c.Id
select new { id = gr.FirstOrDefault().id, Title = gr.FirstOrDefault().Title, Version = gr.FirstOrDefault().Version };
Maybe you can try that :
var result =
from i in dc.TFFiles
where i.Tid == tid
group i by new { i.Title, i.Type } into gr
join c in dc.FCategories on gr.FirstOrDefault().CatId equals c.Id
select new { id = gr.FirstOrDefault().id, Title = gr.FirstOrDefault().Title, Version = (from cv in gr select cv.Version).Max() };

Join two linq queries on one key column to get one result set

Here I want to join the out put of the first query(one column) to the result of the 2nd query to get a one result set. How can I merge them.(CONCAT doesn't work as required. eg: var query2 = query.concat(query1);)
var query = (from PP in _db.paymentPlans
join APP in _db.Applications on PP.applicationID equals APP.ApplicationId
join C in _db.Courses on APP.courseID equals C.courseID
where PP.active == true && APP.agentID == agentID
orderby C.courseID ascending
group new {C,PP} by new {C.courseID} into totalRecievable
select new PdPpAppCourseModel
{
courseID = totalRecievable.Key.courseID,
totalAmount = totalRecievable.Sum(x => x.PP.totalAmount)
}).ToList();
var query1=(from PD in _db.paymentDetails
join PP in _db.paymentPlans on PD.paymentPlanID equals PP.paymentPlanID
join APP in _db.Applications on PP.applicationID equals APP.ApplicationId
join C in _db.Courses on APP.courseID equals C.courseID
where PP.active == true && APP.agentID == agentID
orderby C.courseID ascending
group new { C,PD } by new { C.courseID, C.cricosCode, C.courseName } into paymentsCourseWise
select new PdPpAppCourseModel
{
courseID = paymentsCourseWise.Key.courseID,
cricosCode = paymentsCourseWise.Key.cricosCode,
courseName = paymentsCourseWise.Key.courseName,
paidAmount = paymentsCourseWise.Sum(x => x.PD.paidAmount)
}).ToList();
You could join query1 and query like this
var result = (from q1 in query1
join q in query on q1.courseID = q.courseID
select new PdPpAppCourseModel
{
courseID = q1.Key.courseID,
cricosCode = q1.Key.cricosCode,
courseName = q1.Key.courseName,
paidAmount = q1.Sum(x => x.PD.paidAmount),
totalAmount = q.totalAmount
}).ToList();

Translate nested select of oracle query to linq

I was given an oracle query and am trying to translate it to linq. I have been successful up to the point of the inner select.
select distinct pm.projNumber, nvl(s.submission_nbr, ppo.submission_nbr) submission_nbr, nvl(ot.objectname, pot.objectname) objectname , wa.assigned, f.lname, f.fname
from wfassignment wa
join CodeTab c on c.codeid = wa.appr_stat
join projobject po on po.objectid = wa.objectid
join projmain pm on pm.projid = po.projid
join hhistory s on s.Prop_no = pm.Prop_No and s.Inst_Code = pm.Inst_Code and pm.System = 'foo' and s.mark = po.mark
left join objecttypes ot on ot.objecttype = po.objecttype and ot.system = pm.system
left join(
select a.mark, a.objecttype, a.submitdate, c.submission_nbr
from projobject a
join b on b.projid = a.projid
join c on c.Prop_no = b.Prop_No and c.Inst_Code = b.Inst_Code and b.System = 'foo' and c.mark = a.mark ) ppo on ppo.mark = s.parentmark
left join objecttypes pot on pot.objecttype = ppo.objecttype and pot.system = pm.system
join faculty f on f.unique_id = wa.unique_id
where wa.completed is null and wa.unique_id in ('foo', 'bar', 'foo', 'bar')
below is my C# code up until "left join (select... )"
var pageObject = (
from wa in db.WFASSIGNMENTs
join c in db.CODETABs on wa.APPR_STAT equals c.CODEID
join po in db.PROJOBJECTs on wa.OBJECTID equals po.OBJECTID
join pm in db.PROJMAINs on po.PROJID equals pm.PROJID
join s in db.HHISTORies on new { pm.PROP_NO, pm.INST_CODE, po.MARK } equals new { s.PROP_NO, s.INST_CODE, s.MARK }
join ot in db.OBJECTTYPES on new { OBJECTTYPE = po.OBJECTTYPE, pm.SYSTEM } equals new { OBJECTTYPE = ot.OBJECTTYPE1, ot.SYSTEM }
How do I translate the inner select to linq?
I pulled out the inner select into another variable and just joined that variable. Did the trick.

Translate SQL statement with SUM() in Entity

I try to translate this SQL code :
SELECT w.Id, w.LastName, w.FirstName, SUM(d.Price*dt.Number) AS somme
FROM Waiter w
INNER JOIN Client c on w.Id = c.WaiterId
INNER JOIN DisheOnTable dt on c.Id = dt.ClientId
INNER JOIN Dishe d on dt.DisheId = d.Id
GROUP BY w.Id, w.LastName, w.FirstName
ORDER BY somme DESC;
in entity framework.
I tried something like this
var query2 = (from w in db.Waiter
join c in db.Client on w.Id equals c.WaiterId
join dt in db.DisheOnTable on c.Id equals dt.ClientId
join d in db.Dishe on dt.DisheId equals d.Id
group w by new { w.Id, w.LastName, w.FirstName } into g
//orderby g.Select() descending
select new
{
id = g.Key.Id,
lastname = g.Key.LastName,
firstname = g.Key.FirstName,
total = g.Sum(q => q.)
});
but my sum doesn't work (after multiple research and try) and i don't know how to multiply my variables.
PS : The SQL statement works well, i tried it.
Thank you for helping guys ! :)
You need to group on both dish and DishOnTable alias as Price is in Dish and Number is in DishOnTable:
group new{ d,dt} by new {w.Id, w.LastName, w.FirstName} into g
and now sum the columns which you want from it
select new {
id = g.Key.Id,
lastname = g.Key.LastName,
firstname = g.Key.FirstName,
total = g.Sum(q => q.d.Price * q.dt.Number)
}).OrderBy(x=>x.total)

Categories