
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to use “not in” query with C# LINQ?
Except operator are designed to allow you to query data which supports the IEnumerable<T< interface. Since all LINQ query expressions, and most LINQ queries, return IEnumerable<T<, these operators are designed to allow you to perform set operations on the results of a LINQ query.
The Except operator shows all the items in one list minus the items in a 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(); } }
Here in the above example we have 2 list and we are fetching only those result from the list A which are not present in listb
Output
1 2 5 6
Example 2
Using 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
- Related Articles
- How to use LINQ in C#?
- How to make use of Join with LINQ and Lambda in C#?
- How to use LINQ to sort a list in C#?
- C# Program to Generate Random Even Numbers Using LINQ Parallel Query
- How to make use of both Take and Skip operator together in LINQ C#?
- How to make use of both Take and Skip operator together in LINQ C# Programming
- How to use count with CASE condition in a MySQL query?
- How to use prepared statement for select query in Java with MySQL?
- How to flatten a list using LINQ C#?
- How to use MySQL LIKE query to search a column value with % in it?
- How to use SELECT Query in Android sqlite?
- How to use alias in MySQL select query?
- How to query MongoDB a value with $lte, $in and $not to fetch specific values?
- When to use inline function and when not to use it in C/C++?
- How to use CHAR_LENGTH() in MySQL CREATE TABLE query?

Advertisements