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

Ray*_*Ray 1 c# linq where

我无法理解如何正确使用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>();
Run Code Online (Sandbox Code Playgroud)

我想这样做:

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>();
Run Code Online (Sandbox Code Playgroud)

Sel*_*enç 5

你可以Contains改用:

!ListOfNotAllowedStage.Contains(FunTable.ft_FunStage)
Run Code Online (Sandbox Code Playgroud)

或者Any:

!ListOfNotAllowedStage.Any(x => x == FunTable.ft_FunStage)
Run Code Online (Sandbox Code Playgroud)