
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- How to query MongoDB with “like”?
- How to work with “!=” or “not equals” in MySQL WHERE?
- How to use LINQ in C#?
- MongoDB Query for boolean field as “not true”
- Does it make sense to use “LIMIT 1” in a query “SELECT 1 …”?
- DFA for Strings not ending with “THE” in C++?
- “Toggle” query in MongoDB?
- How to query MongoDB similar to “like” ?
- Can we use “LIKE concat()” in a MySQL query?
- Can we use “IF NOT IN” in a MySQL procedure?
- How to deal with “could not find function” error in R?
- Using “not equal” in MySQL?
- To “user-scalable=no” or not to “user-scalable=no” in HTML5
- How to replace “and” in a string with “&” in R?
- MongoDB: How to query a collection named “version”?
Advertisements