SoFunction
Updated on 2025-03-06

Intersect, Except and Distinct clauses in the basic LINQ

Intersect clause

1. Introduction

Intersect returns intersection, which refers to elements that appear in two sets at the same time, and the same function as the Intersect method in the database.

2. Case

var q =  (from c in             
          select          
         ).Intersect(             
                    from e in              
                    select 
         );

Except clause

1. Introduction

Except returns a difference set, which refers to an element located in one set but not in another set. Except is to remove the data in the first set that appears in the second set.

2. Case

Case 1:

var q = (from c in     
        select 
        ).Except(from e in      
                 select 
 );

Case 2:

//1 2 These two records
var q1 = from s in 
      where  < 3
      select s;

 //1 2 3 4 These four recordsvar q2 = from s in 
      where  < 5
      select s;

var r = (q2).ToList();// nullvar r2 = (q1).ToList();//3 4

Distinct clause

1. Introduction

The sequence returned by Distinct contains the unique elements of the input sequence, and the statement is a single collection operation.

2. Case

List<int> list = new List<int>() {1,2,3,3,3};
var result = ();

The result of Result is: {1,2,3}

This is all about this article about LINQ using Intersect, Except and Distinct clauses. I hope it will be helpful to everyone's learning and I hope everyone will support me more.