Linq下的distinct()比SQLServer下的distinct更强大,更自由,呵呵

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介:

大家好,今天调点时间来说一下LINQ里的distinct(),以及解决过滤重复记录的方法

准备数据:先来个实体类,自己为它赋值,然后用 linq to object对象它进行distinct的操作

public abstract class BaseEntity
   {
       public BaseEntity() : this(0) { }

       public BaseEntity(long id)
       {
           ID = id;
       }

       public long ID { get; private set; }
   }
   class People : BaseEntity
   {
       public string UserName { get; set; }
       public string Email { get; set; }
       public int Age { get; set; }
   }
        #region linq to object 
         List<People> peopleList = new List<People>();
            peopleList.Add(new People { UserName = "zzl", Email = "1" });
            peopleList.Add(new People { UserName = "zzl", Email = "1" });
            peopleList.Add(new People { UserName = "lr", Email = "2" });
            peopleList.Add(new People { UserName = "lr", Email = "2" });

            Console.WriteLine("用扩展方法可以过滤某个字段,然后把当前实体输出");
            peopleList.DistinctBy(i => new { i.UserName }).ToList().ForEach(i => Console.WriteLine(i.UserName + i.Email));
            Console.WriteLine("默认方法,集合中有多个字段,当所有字段发生重复时,distinct生效,这与SQLSERVER相同");
            peopleList.Select(i => new { UserName = i.UserName, Email = i.Email }).OrderByDescending(k => k.Email).Distinct().ToList().ForEach(i => Console.WriteLine(i.UserName + i.Email));
            Console.WriteLine("集合中有一个字段,将这个字段重复的过滤,并输出这个字段");
            peopleList.Select(i => new { i.UserName }).Distinct().ToList().ForEach(i => Console.WriteLine(i.UserName));

            #endregion
 

下面我们来看一下linq to sql的测试情况,看代码:

        #region 对linq to sql进行Ddistinct()
         DataClasses1DataContext db = new DataClasses1DataContext();
            Console.WriteLine("对单个字段可以实现");
            db.Customer.Select(i => new { i.Name }).Distinct().ToList().ForEach(i => Console.WriteLine(i.Name));
            Console.WriteLine("直接对原-数据集中多个字段进行过滤");
            db.Customer.ToList().Select(i => new { Name = i.Name, Email = i.Email }).Distinct().ToList().ForEach(i =>
            {
                Console.WriteLine(i.Name + i.Email);
            });
            Console.WriteLine("将linq to sql对象赋给别一个实体对象时出现了不能过滤的问题");
            List<Customermodel> entity = new List<Customermodel>();
            db.Customer.ToList().Select(i => new { Name = i.Name, Email = i.Email }).ToList().ForEach(i =>
            {
                entity.Add(new Customermodel { Name = i.Name, Email = i.Email });
            });
            entity.Distinct().ToList().ForEach(i => Console.WriteLine(i.Name + i.Email));
            Console.WriteLine("使用distinct扩展方法进行过滤,没有问题");
            entity.DistinctBy(j => new { j.Name }).ToList().ForEach(i => Console.WriteLine(i.Name + i.Email));
            #endregion
测试结果:
 
 

最后,贡献出DistinctBy这个IEnumerable的扩展方法:

image

本文转自博客园张占岭(仓储大叔)的博客,原文链接:Linq下的distinct()比SQLServer下的distinct更强大,更自由,呵呵,如需转载请自行联系原博主。

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
SQL .NET 数据库
一步一步学Linq to sql(二):DataContext与实体
DataContext  DataContext类型(数据上下文)是System.Data.Linq命名空间下的重要类型,用于把查询句法翻译成SQL语句,以及把数据从数据库返回给调用方和把实体的修改写入数据库。
1105 0
|
SQL 存储 开发框架
Linq To SQl总结
Linq To SQl总结
149 0
Linq To SQl总结
|
SQL .NET Go
艾伟_转载:LINQ to SQL、NHibernate比较(二)-- LINQ to SQL实例
用ADO.NET操作数据库大家一定再熟悉不过了,select、insert、update等等SQL语句大家也都必然滚瓜烂熟。我将自己在学习LINQ to SQL过程中的动手经历记录下来,作为今后学习的参考,也希望对刚刚接触的人有一点帮助。
1434 0
|
SQL JavaScript .NET

相关课程

更多