简体   繁体   English

从两个相关列表中使用特定条件进行LINQ查询

[英]LINQ query from two related lists with specific criteria

Using SQL Server and C#: I have 3 tables: Employee ( EmployeeId, JobDescription ), CashAllowance ( CashAllowanceId ) and EmployeeCashAllowance ( EmployeeId, CashAllowanceId, ValueTaken, DateAdded ). 使用SQL Server和C#:我有3个表:Employee( EmployeeId,JobDescription ),CashAllowance( CashAllowanceId )和EmployeeCashAllowance( EmployeeId,CashAllowanceId,ValueTaken,DateAdded )。 Employee has (EmployeeeId) as primary key, CashAllowance has (CashAllowanceId) as primary key, EmployeeCashAllowance has 2 foreign keys (EmployeeId and CashAllowanceId) related to the first 2 tables. Employee具有(EmployeeeId)作为主键,CashAllowance具有(CashAllowanceId)作为主键,EmployeeCashAllowance具有2个与前两个表相关的外键(EmployeeId和CashAllowanceId)。

I need to get the list of ( EmployeeCashAllowance ) in a specific date + for specific CashAllowanceId + for employees having JobDescription = "Dev" 我需要获取特定日期的清单( EmployeeCashAllowance )+特定CashAllowanceId +具有JobDescription =“ Dev”的员工

I need to achieve this in a LINQ query on lists filled from DB where list of all EmployeeCashAllowance is a property of the Employee object (each Employee object has List ListEmployeeCashAllowances as a property). 我需要在数据库填充的列表上的LINQ查询中实现此目的,其中所有EmployeeCashAllowance的列表都是Employee对象的属性(每个Employee对象都有List ListEmployeeCashAllowances作为属性)。 What I wrote was this: 我写的是这样的:

var sumValues = (from e in Employees
                        where (e.JobDescription == "Dev")
                        from c in e.ListEmployeeCashAllowances
                        where (c.EmployeeId == e.EmployeeId && c.CashAllowanceId == selectedCashAllowanceId && c.DateAdded == selectedDate)
                        select c).ToList();

But this is not working as I expected, it's returning all rows in Employee and EmployeeCashAllowance whatever the selected criteria is (even is JobDescription is not Dev and CashAllowanceId is not the selectedCashAllowanceId). 但这不能按我预期的那样工作,无论选择的条件是什么,它都会返回Employee和EmployeeCashAllowance中的所有行(即使JobDescription不是Dev并且CashAllowanceId也不是selectedCashAllowanceId)。

Where did I go wrong with this? 我在哪里出错了?

You better use join I suppose try something like this , I haven't tested so it must look like this 您最好使用join,我想尝试一下这样的东西,我还没有测试过,所以它一定看起来像这样

var sumValues = (from e in Employees
                    join c in EmployeeCashAllowances on e.EmployeeId equals c.EmployeeId 
                    where ( c.CashAllowanceId == selectedCashAllowanceId && c.DateAdded == selectedDate && e.JobDescription == "Dev")
                    select c).ToList();

Not Tested, Just gave a try 未测试,尝试一下

var results = from e in Employees
              from ec in e.EmployeeCashAllowances
              where (e.EmployeeId == ec.EmployeeId && ec.CashAllowanceId == selectedCashAllowanceId && ec.DateAdded == selectedDate && e.JobDescription == "Dev")
              select ec;

Well, I didn't expect this, but I had to change from the query to get what I want, with the restriction that I had use "Sum" and get sum of values in the list of EmployeeCashAllowance resulted: 好吧,我没有想到这一点,但是我不得不从查询中更改以获取所需的内容,但限制是我必须使用“ Sum”并在EmployeeCashAllowance列表中获取值的总和:

Employees.SelectMany(e => e.ListEmployeeCashAllowances).Where(lc => lc.CashAllowanceId == selectedCashAllowanceId).Select(c => c.ValueTaken).Sum();

Note that if I don't use Sum the list returned will contain all items in all the ListEmployeeCashAllowances. 请注意,如果我不使用Sum,返回的列表将包含所有ListEmployeeCashAllowances中的所有项目。

It becomes clear when you clean the formatting: 当您清除格式时,它将变得很清楚:

var sumValues =
    from e in Employees
    where
        e.JobDescription == "Dev"
    from c in e.ListEmployeeCashAllowances // Gotcha!!!
    where
        c.EmployeeId == e.EmployeeId &&    // Is that really necessary?
        c.CashAllowanceId == selectedCashAllowanceId &&
        c.DateAdded == selectedDate
    select c;

Normally I use LINQ methods like: 通常我使用LINQ方法,例如:

var query = Employees
    .Where(e => e.JobDescription == "Dev")
    .SelectMany(e => e.ListEmployeeCashAllowances)
    .Where(c =>
        c.CashAllowanceId == selectedCashAllowanceId &&
        c.DateAdded == selectedDate);

But 'e.ListEmployeeCashAllowances' still can be selecting all users... A long shot, without knowing your environment, could be: 但是'e.ListEmployeeCashAllowances'仍然可以选择所有用户...长时间不了解您的环境,可能是:

var query = Employees
    .SelectMany(e => e.ListEmployeeCashAllowances)
    .Where(c =>
        c.JobDescription == "Dev" &&
        c.CashAllowanceId == selectedCashAllowanceId &&
        c.DateAdded == selectedDate);

Resolving what to do with 'query', you can simply do: 解决“查询”的问题,您可以简单地执行以下操作:

var sumValues = query.Sum(c => c.ValueTaken);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM