IQueryable 继承自IEnumerable

先举例:

#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

该扩展方法贴出:

public static class EnumerableExtensions
{
public static IEnumerable<TSource> DistinctBy<TSource, Tkey>(this IEnumerable<TSource> source, Func<TSource, Tkey> keySelector)
{
HashSet<Tkey> hashSet = new HashSet<Tkey>();
foreach (TSource item in source)
{
if (hashSet.Add(keySelector(item)))
{
yield return item;
}
}
}
}

LINQ里的Distinct()的更多相关文章

  1. 重写类的Equals以及重写Linq下的Distinct方法

    当自定义一个类的时候,如果需要用到对比的功能,可以自己重写Equals方法,最整洁的方法是重写GetHashCode()方法. 但是,这个方法只适用于对象自身的对比(如if(a==b))以及字典下的C ...

  2. Linq 中的distinct去重

    Linq的Distinct和T-Sql的distinct一样,可以将重复的结果集去重注意: 1 distinct去重记录要求每个字段都重复时,才算重复对象,这与sql一样2 distinct语句可以和 ...

  3. linq group by / distinct

    https://www.cnblogs.com/qixu/p/6033532.html http://www.cnblogs.com/A_ming/archive/2013/05/24/3097062 ...

  4. Linq里where出现null的问题

    今天遇到一个问题,怎么在where里判断一个字段是否为null,并且这个字段不是字符串string类型,而是int和GUID类型,折腾了半天终于搞明白了.(由于项目是我半路接手的,问题是前期的同事给我 ...

  5. linq里的select和selectmany操作

    Select() 和 SelectMany() 的工作都是依据源值生成一个或多个结果值.Select() 为每个源值生成一个结果值.因此,总体结果是一个与源集合具有相同元素数目的集合.与之相反,Sel ...

  6. linq里lambda写的join查询,并附加动态拼接的条件,条件为enum类型的查询

    因为查询条件不固定的原因,sql式的linq查询没法动态拼接条件. 网上搜的资料整理之后终于解决. 参考资料: enum使用 http://blog.csdn.net/slowlifes/articl ...

  7. 用于Linq的去重 Distinct

            /// <summary>         /// 用于Linq的去重,扩展方法需要放到静态类中         /// </summary>          ...

  8. linq里的select和selectmany操作 投影运算

    原文地址:https://msdn.microsoft.com/zh-cn/library/bb546168.aspx#Mtps_DropDownFilterText 投影运算   其他版本   投影 ...

  9. LINQ里的“equals”和“==”的区别

    对于值类型,如果对象的值相等,则相等运算符 (==) 返回 true,否则返回 false.对于string 以外的引用类型,如果两个对象引用同一个对象,则 == 返回 true.对于 string ...

随机推荐

  1. [King.yue]EXT.Grid行双击事件

    .Listeners(l => {l.CellDblClick.Handler = string.Format(@"alert('xx');"); })

  2. [Andrew]Ext.net Grid常用js

    var gridFunction= function (gridId) {                //获取当前Grid                var gridView = Ext.ge ...

  3. 学习Python前序

    最近一直在学习有关Python语言.回顾的时候,发现学习过程中的有些东西被遗漏了.故记录在此......加深记忆,方便查找. The reason:     语言如此多,why choose Pyth ...

  4. Selenium 处理windows 上传 窗口

    selenium无法控制windows窗口,故需要引用第三方工具autoit. 在如下网址,下载并安装 http://www.autoitscript.com/site/autoit/ 安装autoi ...

  5. 【CSS】Intermediate2:Grouping and Nesting

    1.Grouping 2.Nesting If the CSS is structured well, there shouldn’t be a need to use many class or I ...

  6. linux获取目录下文件

    查看当前目录下的文件: find . -type f 查看当前目录下的文件夹: find . -type d 如果文件file1不为空: if [ -s file1 ];then      echo  ...

  7. POJ 3468 A Simple Problem with Integers 线段树 区间更新

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...

  8. HW3.22

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  9. hadoop-2.6.0.tar.gz + spark-1.5.2-bin-hadoop2.6.tgz 的集群搭建(3节点和5节点皆适用)

    本人呕心沥血所写,经过好一段时间反复锤炼和整理修改.感谢所参考的博友们!同时,欢迎前来查阅赏脸的博友们收藏和转载,附上本人的链接.http://www.cnblogs.com/zlslch/p/584 ...

  10. 问题-Error creating object. Please verify that the Microsoft Data Access Components 2.1(or later) have been properly installed.

    问题现象:软件在启动时报如下错误信息:Exception Exception in module zhujiangguanjia.exe at 001da37f. Error creating obj ...