LINQ left outer join with exclusion - c#

I'm trying to exclude the results of a first query from being included in the results of my second query.
The SQL equivalent is here, where A is my current query, and B is the old query.
I've been trying to use this guide to do the left join, but I can't seem to figure out how it should work in my case. I'm just not understanding how this should work (I can't get the syntax highlighting to be happy).
var emp = from employee in empl
join jc in jce on callout.job_class_code_fk equals jc.job_class_code_fk
join av in ab on employee.employee_id_pk equals av.employee_id_fk
join sh in sho on employee.employee_id_pk equals sh.employee_id_fk into lj
from rnd2 in lj.DefaultIfEmpty()
orderby employee.seniority descending
select new
{
eid = employee.employee_id_pk,
sen = employee.seniority,
nam = employee.employee_name,
pho = employee.phone_number,
lje = sho == null ? sho.employee_id_fk : null //left outer join with exclusion??
};
EDIT:: Based on the suggestions in the comments I've tried both of the following. While I don't have syntax issues any more, neither of the following return ANY results, so there's still something wrong here.
var emp = from employee in empl
join jc in jce on callout.job_class_code_fk equals jc.job_class_code_fk
join av in ab on employee.employee_id_pk equals av.employee_id_fk
join sh in sho on employee.employee_id_pk equals sh.employee_id_fk into lj
from rnd2 in lj.DefaultIfEmpty() where sho == null
orderby employee.seniority descending
select new
{
eid = employee.employee_id_pk,
sen = employee.seniority,
nam = employee.employee_name,
pho = employee.phone_number,
};
var emp = from employee in empl
join jc in jce on callout.job_class_code_fk equals jc.job_class_code_fk
join av in ab on employee.employee_id_pk equals av.employee_id_fk
join sh in sho on employee.employee_id_pk equals sh.employee_id_fk into lj
from rnd2 in lj.DefaultIfEmpty() where rnd2 == null
orderby employee.seniority descending
select new
{
eid = employee.employee_id_pk,
sen = employee.seniority,
nam = employee.employee_name,
pho = employee.phone_number,
};

Okay so (to me anyway) the answer that is the easiest to read and understand ended up being this.
Create two lists, the one I want to exclude, and the master list.
They we run master.Except(exclude) and voila. We've accomplished the effect of a left outer join with exclusion.
Here's the working code. The solutions above could very well have worked, as there was another problem with how the first list was being put together.
var ex = from employee in empl
join sh in sho on employee.employee_id_pk equals sh.employee_id_fk
select new
{
eid = employee.employee_id_pk,
sen = employee.seniority,
nam = employee.employee_name,
pho = employee.phone_number,
};
ex.Distinct();
//get a list of employees who have the enabled orientations and job classifications
var emp = from employee in empl
join jc in jce on employee.employee_id_pk equals jc.employee_id_fk
join av in ab on employee.employee_id_pk equals av.employee_id_fk
orderby employee.seniority descending
select new
{
eid = employee.employee_id_pk,
sen = employee.seniority,
nam = employee.employee_name,
pho = employee.phone_number,
};
emp = emp.Distinct();
emp = emp.Except(ex);

Related

Selecting max in linq query or rewriting to method chain syntax

I managed to turn this SQL query:
SELECT c.carId, c.Codename, count(c.CarId) as [CarCount],
FROM [DbEfTesting].[dbo].[Cars] c
left join Accessories a on c.CarId = a.CarId
left join CarsPeople cp on cp.CarId = c.CarId
left join People p on cp.PersonId = p.PersonId
group by c.CarId, c.Codename
into a LINQ query:
var x = from c in _context.Cars
join a in _context.Accessories on c.CarId equals a.Car.CarId
join j in _context.CarsPeople on c.CarId equals j.CarId
join p in _context.People on j.PersonId equals p.PersonId
group c by new { c.CarId, c.Codename } into g
select new VMCarAggregate()
{
CarId = g.Key.CarId,
Codename = g.Key.Codename,
CarCount = g.Count()
};
But now I'm lost trying to include a max value e.g the SQL:
SELECT c.carId, c.Codename, count(c.CarId) as [CarCount], max(a.AccessoryId) ...
I googled it and found lots of answers for method syntax. If I were using method chain syntax, I know I could do something like this:
_context.Accessories.Max(a => a.AccessoryId);
but I can't figure out how to do the group by in method chain syntax so either:
How can I convert that query to method syntax?
or
How can I inject a select on the max a.AccessoryId in the LINQ query format?
Try the below code once:
var x = from c in _context.Cars
join a in _context.Accessories equals a.Car.CarId
join j in _context.CarsPeople on c.CarId equals j.CarId
join p in _context.People on j.PersonId equals p.PersonId
group new { c.CarId, c.Codename, a.AccesoryId } by new { c.CarId, c.Codename } into g
select new
{
CarId = g.Key.CarId,
Codename = g.Key.Codename,
CarCount = g.Count(),
MaxAccesory = g.Max(z => z.AccesoryId)
};

join returns 0 using DefaultIfEmpty()

I have Query contains more than one left inner join and returns List
it join with table PayrollTransactions it returns o as it have no data wtuth this condention i need the list to be return in all cases even when the second join is empty
public List<PayrollElementsViewModel> GetAllPayrollRunDetails(int? PayrollrollRunID)
{
IQueryable<PayrollElementsViewModel> List =
(from R in database.PayrollElements
where R.Deleted == false
&& R.PayrollElementsPayrollRunID == PayrollrollRunID
join Emp in database.Employee on R.PayrollElementsIDEmployeeID equals Emp.EmployeeID
into g
from Emp in g.DefaultIfEmpty()
join tran in database.PayrollTransactions on Emp.EmployeeID equals tran.PayrollTransactionsEmployeeID
into g6
from tran in g6.DefaultIfEmpty()
where tran.PayrollTransactionsPayrollRunID == PayrollrollRunID
select new PayrollElementsViewModel
{
PayrollElementsPayrollRunID = PayrollrollRunID,
PayrollElementsEmployeeID = Emp.EmployeeID,
PayrollElementsEmployeeName = Emp.EmployeeName,
PayrollElementsEmployeeFingerPrint = Emp.EmployeeFingerPrint,
PayrollElementsStartDate = R.PayrollElementsStartDate,
PayrollElementsEndDate = R.PayrollElementsEndDate,
PayrollElemenTsransactionsValue = tran.PayrollTransactionsValue
});
var results = List.ToList();
return (results);
}
i need to return List contains data as it returns 0 when the join with payrolltransation if its it contains o
it solved by moving The condition in second join to the select to be
join tran in database.PayrollTransactions on Emp.EmployeeID equals tran.PayrollTransactionsEmployeeID
into g6
from tran in g6.DefaultIfEmpty()
select new PayrollElementsViewModel
{
PayrollElemenTsransactionsValue = tran.PayrollTransactionsPayrollRunID == PayrollrollRunID?tran.PayrollTransactionsValue : 0,
});

LINQ query with sub-query on LEFT JOIN conditions

I have these queries in SQL and LINQ that were built to retrieve the same data. Unfortunately they are retrieving different amount of records (LINQ returns 1555 values, and SQL returns 1969) and I can't figure out why.
Please help me to find out what I'm missing. Follows the queries:
SQL:
SELECT l.Lease_Detail_ID, l.Lease_ID, l.XRef_Lease_ID, v.Vendor_Name, l.Description, c.County, l.Amount, l.Payment_Due_Date,
l.Lease_Type, l.Location_ID, l.Active, l.Expiration_Date, a.Authorized, p.Payment_Date
FROM tblfLeaseDetail AS l
LEFT JOIN tblvVendor AS v ON l.Vendor_ID = v.Vendor_ID
LEFT JOIN tblvCounty AS c ON l.County_ID = c.County_ID
LEFT JOIN tblfAuthorization AS a ON l.Lease_Detail_ID = a.Lease_Detail_ID
AND a.Authorization_ID = (SELECT TOP 1 Authorization_ID
FROM tblfAuthorization
WHERE Lease_Detail_ID = l.Lease_Detail_ID
ORDER BY Authorized_Date)
LEFT JOIN tblfPayment AS p ON l.Lease_Detail_ID = p.Lease_Detail_ID
AND p.Payment_ID = (SELECT TOP 1 Payment_ID
FROM tblfPayment
WHERE Lease_Detail_ID = l.Lease_Detail_ID
ORDER BY payment_date)
ORDER BY l.Lease_Detail_ID
LINQ: (Edited after a few comments)
var leaseList = (from l in leases.tblfLeaseDetails
join v in leases.tblvVendors on l.Vendor_ID equals v.Vendor_ID into lv
from jlv in lv.DefaultIfEmpty()
join c in leases.tblvCounties on l.County_ID equals c.County_ID into lc
from jlc in lc.DefaultIfEmpty()
join a in leases.tblfAuthorizations on l.Lease_Detail_ID equals a.Lease_Detail_ID into la
from jla in la.DefaultIfEmpty()
where jla.Authorization_ID == (from aj in leases.tblfAuthorizations
where aj.Lease_Detail_ID == l.Lease_Detail_ID
orderby aj.Authorized_Date ascending
select aj.Authorization_ID).FirstOrDefault()
join p in leases.tblfPayments on l.Lease_Detail_ID equals p.Lease_Detail_ID into lp
from jlp in lp.DefaultIfEmpty()
where jlp.Payment_ID == (from pj in leases.tblfPayments
where pj.Lease_Detail_ID == l.Lease_Detail_ID
orderby pj.Payment_Date ascending
select pj.Payment_ID).FirstOrDefault()
select new LeaseViewModel()
{
Lease_Detail_ID = l.Lease_Detail_ID,
Lease_ID = l.Lease_ID,
XRef_Lease_ID = l.XRef_Lease_ID,
Vendor_Name = jlv.Vendor_Name,
Description = l.Description,
County = jlc.County,
Amount = l.Amount,
Payment_Due_Date = l.Payment_Due_Date,
Lease_Type = l.Lease_Type.ToString(),
Location_ID = l.Location_ID,
Active = l.Active,
Expiration_Date = l.Expiration_Date,
Authorized = jla.Authorized,
Payment_Date = jlp.Payment_Date
});
EDIT:
After analyzing the run-time SQL query generated by LINQ statement I found out that it's creating the Authorized sub-query in the wrong place. Here is what it looks like:
SELECT [t0].[Lease_Detail_ID], [t0].[Lease_ID], [t0].[XRef_Lease_ID], [t1].[Vendor_Name] AS [Vendor_Name], [t0].[Description], [t2].[County] AS [County], [t0].[Amount], [t0].[Payment_Due_Date], [t0].[Expiration_Date], [t3].[Authorized] AS [Authorized], CONVERT(NVarChar(1),[t0].[Lease_Type]) AS [Lease_Type], [t0].[Location_ID], CONVERT(Int,[t0].[Active]) AS [Active], [t4].[Payment_Date] AS [Payment_Date]
FROM [dbo].[tblfLeaseDetail] AS [t0]
LEFT OUTER JOIN [dbo].[tblvVendor] AS [t1] ON [t0].[Vendor_ID] = ([t1].[Vendor_ID])
LEFT OUTER JOIN [dbo].[tblvCounty] AS [t2] ON [t0].[County_ID] = ([t2].[County_ID])
LEFT OUTER JOIN [dbo].[tblfAuthorization] AS [t3] ON ([t0].[Lease_Detail_ID]) = [t3].[Lease_Detail_ID]
LEFT OUTER JOIN [dbo].[tblfPayment] AS [t4] ON ([t0].[Lease_Detail_ID]) = [t4].[Lease_Detail_ID]
WHERE ([t4].[Payment_ID] = ((SELECT TOP (1) [t5].[Payment_ID] FROM [dbo].[tblfPayment] AS [t5] WHERE [t5].[Lease_Detail_ID] = ([t0].[Lease_Detail_ID])
ORDER BY [t5].[Payment_Date] )))
AND ([t3].[Authorization_ID] = (( SELECT TOP (1) [t6].[Authorization_ID]
FROM [dbo].[tblfAuthorization] AS [t6]
WHERE [t6].[Lease_Detail_ID] = ([t0].[Lease_Detail_ID])
ORDER BY [t6].[Authorized_Date] )))
The problem is that it only made more confuse, once Payment and Authorized joins have exactly the same structure.
after some research I finally found how to do it. Here is the LINQ query that generates the SQL I was trying to get:
var leaseList = (from l in leases.tblfLeaseDetails
join p in leases.tblfPayments
on l.Lease_Detail_ID equals p.Lease_Detail_ID into lp
from jlp in lp.Where(x => x.Payment_ID == (from pj in leases.tblfPayments
where pj.Lease_Detail_ID == l.Lease_Detail_ID
orderby pj.Payment_Date ascending
select pj.Payment_ID).FirstOrDefault()).DefaultIfEmpty()
join a in leases.tblfAuthorizations on l.Lease_Detail_ID equals a.Lease_Detail_ID into la
from jla in la.Where(x => x.Authorization_ID == (from aj in leases.tblfAuthorizations
where aj.Lease_Detail_ID == l.Lease_Detail_ID
orderby aj.Authorized_Date ascending
select aj.Authorization_ID).FirstOrDefault()).DefaultIfEmpty()
join v in leases.tblvVendors on l.Vendor_ID equals v.Vendor_ID into lv
from jlv in lv.DefaultIfEmpty()
join c in leases.tblvCounties on l.County_ID equals c.County_ID into lc
from jlc in lc.DefaultIfEmpty()
select new LeaseViewModel()
{
Lease_Detail_ID = l.Lease_Detail_ID,
Lease_ID = l.Lease_ID,
XRef_Lease_ID = l.XRef_Lease_ID,
Vendor_Name = jlv.Vendor_Name,
Description = l.Description,
County = jlc.County,
Amount = l.Amount,
Payment_Due_Date = l.Payment_Due_Date,
Lease_Type = l.Lease_Type.ToString(),
Location_ID = l.Location_ID,
Active = l.Active,
Expiration_Date = l.Expiration_Date,
Authorized = jla.Authorized,
Payment_Date = jlp.Payment_Date
});

How to use left outer join in LINQ for SQL query?

How can I use left outer join in LINQ for the following SQL query?
SELECT a.EventID, a.PrizeId, b.PrizeName, b.PrizeValue, c.FightID, c.Winnerid, c.WinnerName
FROM tblUserprize a
JOIN tblPrizeDetails b
ON a.PrizeId=b.PrizeId
LEFT OUTER JOIN tblWinnersList c
ON a.EventID=c.EventID AND a.PrizeId=c.PrizeId AND c.FightID = 1534
WHERE a.EventID = 1320
It should look like this:
var userPrize = (
from a in tblUserprize
join b in tblPrizeDetails on a.PrizeId equals b.PrizeId
join c in tblWinnersList on new { a.EventID, a.PrizeId } equals new { c.EventID, c.PrizeId } into joinedTables
from item in joinedTables.DefaultIfEmpty()
where a.EventID == 1320 && item.FightID == 1534
select new
{
a.EventID,
a.PrizeId,
b.PrizeName,
b.PrizeValue,
item.FightID,
item.Winnerid,
item.WinnerName
});

How to execute the following SQL-Query using LINQ or HQL

How can I execute the following query using Castle ActiveRecords and LINQ or HQL?
SELECT a.id, s.classes, COUNT(p.id), MAX(p.date) AS last, MIN(p.date) AS first
FROM account a
LEFT JOIN school s ON s.account_id = a.id
LEFT JOIN user u ON u.account_id = a.id
LEFT JOIN points p ON p.user_id = u.id
WHERE payment = "S"
GROUP BY a.id
The tables are related in the following way:
I also have ActiveRecord classes for all tables with the correct relations defined (if I do the query in steps it works, but it is slow as there are a lot of rows) and I tried the following which didn't worked:
var result = from account in AccountRecord.Queryable
join s in SchoolRecord.Queryable on account equals s.Account into schools
from school in schools.DefaultIfEmpty(null)
join user in UserRecord.Queryable on account equals user.Account
join p in PointsRecord.Queryable on user equals p.User into points
where account.PaymentType == "S"
select new { Account = account, School = school, Count = points.Count() };
which threw the following The method or operation is not implemented-Exception at:
NHibernate.Linq.Visitors.QueryModelVisitor.VisitGroupJoinClause(GroupJoinClause groupJoinClause, QueryModel queryModel, Int32 index)
Found the solution using HQL - I'm still open for a LINQ solution:
HqlBasedQuery query = new HqlBasedQuery(typeof(AccountRecord),
"SELECT a, s, COUNT(p), MIN(p.DateUTC), MAX(p.DateUTC) " +
"FROM AccountRecord a " +
"LEFT JOIN a.Schools s " +
"LEFT JOIN a.Users u " +
"LEFT JOIN u.Points p " +
"WHERE a.PaymentType=:payment GROUP BY a.Id");
query.SetParameter("payment", "S");
var result = from object[] row in (ArrayList)ActiveRecordMediator.ExecuteQuery(query)
select new
{
Account = row[0] as AccountRecord,
School = row[1] as SchoolRecord,
Count = row[2],
First = (new DateTime(1970, 1, 1, 0, 0, 0, 0)).AddSeconds(Convert.ToDouble(row[3])),
Last = (new DateTime(1970, 1, 1, 0, 0, 0, 0)).AddSeconds(Convert.ToDouble(row[4]))
};
I think your Linq query would be something like:
var result = from a in AccountRecord.Queryable
join s in SchoolRecord.Queryable on a.id equals s.account_id
join u in UserRecord.Queryable on a.id equals u.account_id
join p in PointsRecord.Queryable on u.id equals p.user_id
where a.payment == "S"
group by a.id
select new
{
Account = a,
School = s,
Count = p.Count()
};
Though I'm not sure how well NHibernate will handle the group by and Count() combination. You might want to see what the generated SQL ends up being, if it doesn't throw an error.
If that doesn't work, you might want to select the records back, and group/count them in the application instead, more like:
var data = from a in AccountRecord.Queryable
join s in SchoolRecord.Queryable on a.id equals s.account_id
join u in UserRecord.Queryable on a.id equals u.account_id
join p in PointsRecord.Queryable on u.id equals p.user_id
where a.payment == "S"
select new
{
Account = a,
School = s,
Count = c
};
var grouped = data.ToList.GroupBy(x => x.Account.Id);

Categories

Resources