c# - 如何在 where 子句中使用 LINQ Except()

标签 c# linq where-clause

我无法理解如何正确使用 Except()。 我想在 where 子句 中使用 Except() 来排除与字符串列表匹配的结果。我编了下面这个简单的例子来说明。

此外,在这种情况下使用 Except 是最快的方法吗?如果不是,什么会更快?

我在哪里:

var result = (from FunTable in Context.t_FunTable

              where FunTable.ft_FunStartDate >= startDate
              && FunTable.ft_FunEndDate <= endDate

       ---->  && FunTable.ft_FunStage != notAllowedStage1 
       ---->  && FunTabble.ft_FunStage != notAllowedStage2

              select FunTable.ActivityName).ToList<String>();

我想这样做:

var result = (from FunTable in Context.t_FunTable

              where FunTable.ft_FunStartDate >= startDate
              && FunTable.ft_FunEndDate <= endDate

      ---->   && FunTable.ft_FunStage.Except(ListOfNotAllowedStages)

              select FunTable.ActivityName).ToList<String>();

最佳答案

您可以使用 Contains 代替:

!ListOfNotAllowedStage.Contains(FunTable.ft_FunStage)

任何:

!ListOfNotAllowedStage.Any(x => x == FunTable.ft_FunStage)

关于c# - 如何在 where 子句中使用 LINQ Except(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22700008/

相关文章:

c# - 使用 Entity Framework 和 MVVM 将数据库信息从一个 ViewModel 传递到另一个

c# - 如何使用 LINQ (C# 3.0) 更改 IDictionary 的内容

C# 使用 LINQ 读取 XML

c# - LINQ 语句中的条件 IF 语句

c# - 如何在T4模板中输出<Type>

c# - 为什么我的警报没有弹出? ASP.NET C#

c# - 在运行时更改用户及其在 WPF/Prism 桌面应用程序中的权限的常用方法

MySQL - 在 WHERE 子句中按日期排序

MySQL WHERE 与 IF 条件

php - MYSQL PHP - 获取 float LIKE $float