I have a table called Payroll. Payroll can have many PayStubs. In other words there is table called PayStub that is a child entity of Payroll. PayStub has a child Entity called PayrollTax. I want to write a LINQ-to-SQL query that gets all payrolls which have more than one Payroll Tax. I use the following query:
public IList<Payroll> GetPayrollsWithPayrollTaxes()
{
return (from payroll in ActiveContext.Payrolls
join payStub in ActiveContext.PayStubs on payroll.PayrollID equals payStub.PayrollID
where payStub.InternalPayrollTaxes.Count > 0
select payroll
).ToList();
}
The problem is since there is a one to many relationship between Payroll and PayStub, I end up getting the same Payroll twice. I want a unique list of Payrolls.
Any ideas and suggestions are appreciated!
Have you tried
.Distinct().ToList()?Or you could add an
intoafter thejoinlike this: