[英]How can I filter results of one LINQ query based on another?
Given the following:鉴于以下情况:
DP_DatabaseTableAdapters.EmployeeTableAdapter employeetableAdapter = new DP_DatabaseTableAdapters.EmployeeTableAdapter();
DP_Database.EmployeeDataTable employeeTable = employeetableAdapter.GetData();
var leadEmployees = from e in employeeTable
where e.IsLead == true
select e;
DP_DatabaseTableAdapters.LaborTicketTableAdapter tableAdapter = new DP_DatabaseTableAdapters.LaborTicketTableAdapter();
DP_Database.LaborTicketDataTable table = tableAdapter.GetDataByDate(date.ToString("MM/dd/yyyy"));
var totHours = from l in table
join e in leadEmployees on l.EmployeeID equals e.EmployeeID
group l by l.EmployeeID into g
orderby g.Key
select new
{
EmployeeID = g.Key,
HoursWorked = g.Sum(s => s.HoursWorked)
};
Total hours correctly filters the results based on the leadEmployee's list of people who have the IsLead bit set to true. Total hours 根据将 IsLead 位设置为 true 的leadEmployee 的人员列表正确过滤结果。
I would like to know how to do this with a where clause, I have attempd to use leadEmployees.Contanis but it wants a whole EmployeeRow...我想知道如何使用 where 子句来做到这一点,我曾尝试使用leadEmployees.Contanis,但它需要一个完整的 EmployeeRow ...
How can I add what looks to be part of an IN clause to a where filter to replace the join?如何将看起来是 IN 子句的一部分添加到 where 过滤器以替换联接?
var totHours = from l in table
where ??????
group l by l.EmployeeID into g
orderby g.Key
select new
{
EmployeeID = g.Key,
HoursWorked = g.Sum(s => s.HoursWorked)
};
The contains will only want a whole EmployeeRow if you are selecting whole employee roles.如果您选择整个员工角色,则包含只需要整个 EmployeeRow。 You can either:
您可以:
leadEmployees.Select(e => e.id).contains
OR或者
leadEmployees.Count(e => e.id == l.id) > 0
Both will work.两者都会起作用。 (Excuse slightly rushed lack of consideration for syntax accuracies).
(请原谅稍微匆忙地缺乏对语法准确性的考虑)。
This should work:这应该有效:
var leadEmployees = from e in employeeTable
where e.IsLead == true
select e.EmployeeID;
var totHours = from l in table
where leadEmployees.Contains(l.EmployeeID)
group l by l.EmployeeID into g
orderby g.Key
select new
{
EmployeeID = g.Key,
HoursWorked = g.Sum(s => s.HoursWorked)
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.