How to use 'not in' query in C# LINQ?

王林
Release: 2023-09-01 10:29:09
forward
994 people have browsed it

如何在 C# LINQ 中使用“not in”查询?

The Except operator is designed to allow you to query data that supports the IEnumerable

Except operator displays all items in one list minus the items in the second list

Example 1

class Program{
   static void Main(string[] args){
      var listA = Enumerable.Range(1, 6);
      var listB = new List<int> { 3, 4 };
      var listC = listA.Except(listB);
      foreach (var item in listC){
         Console.WriteLine(item);
      }
      Console.ReadLine();
   }
}
Copy after login

In the above example we have two list, and we only get those results from list A that are not in list B

Output

1
2
5
6
Copy after login
Copy after login

Example 2

Use Sql-like syntax

static void Main(string[] args){
   var listA = Enumerable.Range(1, 6);
   var listB = new List<int> { 3, 4 };
   var listC = from c in listA
   where !listB.Any(o => o == c)
   select c;
   foreach (var item in listC){
      Console.WriteLine(item);
   }
   Console.ReadLine();
}
Copy after login

Output

1
2
5
6
Copy after login
Copy after login

The above is the detailed content of How to use 'not in' query in C# LINQ?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template