如何检查一个列表是否包含linq to sql语句中另一个列表的所有元素

时间:2016-08-08 17:13:19

标签: c# sql-server linq linq-to-sql

我有一个任务表,想要将List<int>包含一些personId传递给搜索方法并返回包含所有这些personId的任务(如DoctorId,driverId,NurseId,OperatorId)。我试过这六种方式:

using (MainDataContext context = new MainDataContext())
        {
            return context.Missions
                .Where(t => !personIds.Except(new List<int>() { t.DoctorID, t.NurseID, t.OperatorID, t.DriverID}).Any())
}

2:

!personIds.Any(item => !(new List<int?>() {t.DoctorID, t.NurseID,t.OperatorID,t.DriverID}.Contains(item)))

3:

!personIds.Any(item => t.DoctorID != item && t.NurseID != item && t.OperatorID != item && t.DriverID != item)

4:

personIds.Intersect(new List<int>()  { t.DoctorID, t.NurseID, t.OperatorID, t.DriverID}).Count() == personIds.Count()

5:

personIds.All(u => new List<int?>() { t.DoctorID, t.NurseID, t.OperatorID, t.DriverID }.Contains(u))

6:

personIds.All(p => p == t.DoctorID || p == t.NurseID || p == t.OperatorID || p == t.DriverID)

并且所有这些都引发了同样的错误:

  

除了Contains运算符之外,本地序列不能用于查询运算符的LINQ to SQL实现。

以下代码有效,但它很脏,我希望有更好的方法:

personIds.Count() == 0
                    || (personIds.Count() == 1 && new List<int?>() { t.DoctorID, t.NurseID, t.OperatorID, t.DriverID }.Contains(personIds[0]))

                    || (personIds.Count() == 2 && new List<int?>() { t.DoctorID, t.NurseID, t.OperatorID, t.DriverID }.Contains(personIds[0])
                        && new List<int?>() { t.DoctorID, t.NurseID, t.OperatorID, t.DriverID }.Contains(personIds[1]))

                    || (personIds.Count() == 3 && new List<int?>() { t.DoctorID, t.NurseID, t.OperatorID, t.DriverID }.Contains(personIds[0])
                        && new List<int?>() { t.DoctorID, t.NurseID, t.OperatorID, t.DriverID }.Contains(personIds[1])
                        && new List<int?>() { t.DoctorID, t.NurseID, t.OperatorID, t.DriverID }.Contains(personIds[2]))

                    || (personIds.Count() == 4 && new List<int?>() { t.DoctorID, t.NurseID, t.OperatorID, t.DriverID }.Contains(personIds[0])
                        && new List<int?>() { t.DoctorID, t.NurseID, t.OperatorID, t.DriverID }.Contains(personIds[1])
                        && new List<int?>() { t.DoctorID, t.NurseID, t.OperatorID, t.DriverID }.Contains(personIds[2])
                        && new List<int?>() { t.DoctorID, t.NurseID, t.OperatorID, t.DriverID }.Contains(personIds[3]))

有没有办法检查列表是否包含linq到sql中另一个列表的所有元素?

1 个答案:

答案 0 :(得分:0)

首先,我要确保所有ID都是唯一的。然后,如果有超过4个,那么你不会想要任何结果,所以没有必要进行查询。最后,你只需迭代id并执行Where,确保每个id都是任务ID之一。

using (MainDataContext context = new MainDataContext())
{
    var uniqueIds = personIds.Distinct().ToList();
    if(uniqueIds.Count > 4)
        return Enumerable.Empty<Mission>().AsQueryable();

    var result = context.Missions;
    foreach(int id in uniqueIds)
    {
        result = result.Where(t => id == t.DoctorID || 
                                   id == t.NurseID || 
                                   id == t.OperatorID ||
                                   id == t.DriverID);
    }
    return result;
}

我的假设是,如果你有超过4个唯一ID,你不需要任何结果,如果personIds为空,那么你就需要所有的任务。