Home  >  Article  >  Backend Development  >  How to use 'not in' query in C# LINQ?

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

王林
王林forward
2023-09-01 10:29:09995browse

如何在 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();
   }
}

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

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();
}

Output

1
2
5
6

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete