有没有办法将这两个LINQ语句合并为一个

时间:2015-09-30 21:10:11

标签: c# .net linq

我需要从1个列表中生成2个单独的列表。如果company.IsNone是真的,我希望所有员工都添加到第一个列表中匹配locationsToCompare列表,如果不是,我希望所有员工都不匹配locationsToCompare列表。填充第二个列表时,这恰恰相反。有没有办法将这两个if语句组合成一个,或者我被迫有两个单独的填充两个列表?

var noneEmployees = company.IsNone
       ? employees.Where(employee => locationsToCompare.Any(x => x.ID == employee.LocationID)).ToList()
       : employees.Where(employee => locationsToCompare.All(x => x.ID != employee.LocationID)).ToList();

var locationEmployees = company.IsNone
       ? employees.Where(employee => locationsToCompare.All(x => x.ID != employee.LocationID)).ToList()
       : employees.Where(employee => locationsToCompare.Any(x => x.ID == employee.LocationID)).ToList();

3 个答案:

答案 0 :(得分:2)

员工组取决于是否设置了公司IsNone标志。

但在确定之前,让我们使用ToLookup(TSource, TKey)对员工进行分组,这是一个字典/分组类型查找,其中关键是员工是否在locationsToCompare列表中。

一旦设置了查询,我们就可以在公司IsNone标志中的管道中提取员工。

示例设置数据

var employees = new List<Employee>
         { new Employee { LocationID = 1, Name = "Hamilton" },
           new Employee { LocationID = 2, Name = "Joe"      },
           new Employee { LocationID = 1, Name = "Frank"    },
           new Employee { LocationID = 5, Name = "Reynolds" } };

var company = new Company() { IsNone = false };

<强>地点

var locationsToCompare = new List<Location> { new Location() { ID = 1 },
                                              new Location() { ID = 2 } };

<强>包含

我最终会用Contains代替where子句,所以我会将locationsToCompare提炼成一个id列表。

var LocationIds = locationsToCompare.Select(ltc => ltc.ID);

可以跳过此步骤,但我相信这样可以更轻松地进行维护。

<强> ToLookUp

现在我们使用ToLookup,然后根据公司&#39; IsNone&#39;提取我们的列表。标志:

var empLookup = employees.ToLookup(emp => LocationIds.Contains(emp.LocationID));

var noneEmployees     = empLookup[company.IsNone].ToList();
var locationEmployees = empLookup[!company.IsNone].ToList();

<强>结果

enter image description here

查找是什么样的?因为它为我们带来了沉重的负担。

enter image description here

答案 1 :(得分:1)

使用Except尝试这样的事情,这样您就不必重复所有代码了:

public class Company
{
    public bool IsNone { get; set; }
}

public class Employee
{
    public int LocationID { get; set; }
}

public class Location
{
   public int ID { get; set; }
}

<强>操作

var locationsToCompare = new List<Location> { new Location() { ID = 1 }, new Location() { ID = 2 } };

var employees = new List<Employee> { new Employee() { LocationID = 1 }, new Employee { LocationID = 2 }, new Employee { LocationID = 1}, new Employee { LocationID = 5 } };

var company = new Company();

company.IsNone = false;

var noneEmployees = company.IsNone
                  ? employees.Where(employee => locationsToCompare.Any(x => x.ID == employee.LocationID)).ToList()
                  : employees.Where(employee => locationsToCompare.All(x => x.ID != employee.LocationID)).ToList();

var locationEmployees = employees.Except(noneEmployees).ToList();

答案 2 :(得分:1)

您还可以将位置ID提取到单独的列表中,并将其用于[ { "url" : "https://my_url", "user" : "some_user", "password" : "some_password" }, { "url" : "https://my_url", "user" : "some_other_user", "password" : "some_other_password" } ] 子句中,以及Where()的xor。将它与@ leigh-shepperson的IsNone运算符解决方案相结合,就可以得到:

Except()

我不知道这是否是最具可读性的解决方案,但它可以缩短解决方案。