软件编程
位置:首页>> 软件编程>> C#编程>> LINQ基础之Intersect、Except和Distinct子句

LINQ基础之Intersect、Except和Distinct子句

作者:農碼一生  发布时间:2022-01-19 00:46:13 

标签:LINQ,Intersect,Except,Distinct,子句

Intersect子句

一、简介

Intersect返回交集,交集是指同时出现在两个集合中的元素,和数据库中的Intersect方法实现功能一样。

二、案例

var q =  (from c in db.Customers            
         select c.Age        
        ).Intersect(            
                   from e in db.Employees            
                   select e.Age
        );

Except子句

一、简介

Except返回差集,差集是指位于一个集合但不位于另一个集合的元素。Except是把第一个集合里面的数据 去掉在第二个集合里面出现过的数据。

二、案例

案例一:

var q = (from c in db.Customers    
       select c.Name
       ).Except(from e in db.Employees    
                select e.Name
);

案例二:

//1 2 这两条记录

var q1 = from s in db.Student
     where s.ID < 3
     select s;

//1 2 3 4 这四条记录
var q2 = from s in db.Student
     where s.ID < 5
     select s;

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

Distinct子句

一、简介

Distinct返回的序列包含输入序列的唯一元素,该语句是单个集合操作。

二、案例

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

Result的结果为:{1,2,3}

来源:https://www.cnblogs.com/wml-it/p/14839966.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com