简体   繁体   English

使用 LINQ 在 C# 到 select 一些元素

[英]Using LINQ in C# to select some elements

I'm using LINQ in C# to access some element of a data strucuture.我在 C# 中使用 LINQ 来访问数据结构的某些元素。 Model is my data structure and model.ErrorCollection contains possible errors in the Model. Model 是我的数据结构,model.ErrorCollection 包含 Model 中可能存在的错误。

With the following code I'm able to run my application if there are no errors, except those listed (ErrorType1, ErrorType2, ErrorTypeN) that are tolerated, so if they are found the application runs anyway.使用以下代码,如果没有错误,我可以运行我的应用程序,除了列出的那些(ErrorType1、ErrorType2、ErrorTypeN)是可以容忍的,所以如果发现它们,应用程序仍然会运行。

if (null != model  && !model.ErrorCollection.Any(e =>
!(e is ErrorType1 || e is ErrorType2 || e is ErrorTypeN)) )
 {
  //do something
 }

But what I really need, is the opposite: I want to run my application tolerating any error, except those listed there.但我真正需要的是相反的:我想运行我的应用程序以容忍任何错误,除了那些列出的错误。 If one of them is found, the application must not run.如果找到其中之一,则应用程序不得运行。

Is there a way to do this in LINQ?有没有办法在 LINQ 中做到这一点?

Removing the second negation operator should do it:删除第二个否定运算符应该这样做:

if (model != null && !model.ErrorCollection.Any(
        e => e is ErrorType1 || e is ErrorType2 || e is ErrorTypeN))
{
    //do something
}

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

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