Distinct<TSource>(IEqualityComparer<TSource> comparer) 根据列名来Distinct
1. DistinctEqualityComparer.cs
public class DistinctEqualityComparer<T, V> : IEqualityComparer<T>
{
private Func<T, V> keySelector;
public DistinctEqualityComparer(Func<T, V> keySelector)
{
this.keySelector = keySelector;
}
public bool Equals(T x, T y)
{
return EqualityComparer<V>.Default.Equals(keySelector(x), keySelector(y));
}
public int GetHashCode(T obj)
{
return EqualityComparer<V>.Default.GetHashCode(keySelector(obj));
}
}
2. DistinctExtensions.cs
public static class DistinctExtensions
{
public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source, Func<T, V> keySelector)
{
return source.Distinct(new DistinctEqualityComparer<T, V>(keySelector));
}
}
3. Program.cs
class Program
{
static void Main(string[] args)
{
List<Person> personList = new List<Person>(){
new Person(){Id = 1, Name = "Steven"},
new Person(){Id = 2, Name = "Steven"},
new Person(){Id = 3, Name = "Steven"},
new Person(){Id = 3, Name = "Steven"},
};
List<Person> delegateList = personList.Distinct(x => x.Id).ToList();
}
}
Distinct<TSource>(IEqualityComparer<TSource> comparer) 根据列名来Distinct的更多相关文章
- 数据去重Distinct,IEqualityComparer,IEquatable
很多情况下我们查询数据需要去重重复数据,下面就记录三个去重的方法. Distinct 最基本的去重形式,直接查询出数据后使用Distinct方法进行字段去重. var strList = new Li ...
- 用泛型的IEqualityComparer<T>接口去重复项
提供者:porschev 题目:下列数据放在一个List中,当ID和Name都相同时,去掉重复数据 ID Name 1 张三 1 李三 1 小伟 1 李三 2 李四 2 李武 ----- ...
- EF之通过不同条件查找去重复
Enumerable.Distinct<TSource> Method(IEnumerable<TSource>, IEqualityComparer<TSource&g ...
- 整理一下 System.Linq.Enumerable 类中的那些比较少用的方法
Linq 虽然用得多,但是里面有一些方法比较少用,因此整理一下.Enumerable 类的所有方法可以在 MSDN 上查阅到:https://msdn.microsoft.com/zh-cn/libr ...
- [C#]关于Distinct与重写IEqualityComparer时得知道的二三事
我们在想对一个可枚举的对象集合进行去重操作时,一般第一个想到的就是就是Linq的Distinct方法. 先定义一个类,然后使用Distinct方法去重 class Man { public int A ...
- Enumerable.Union<TSource> 方法
功能:生成两个序列的并集(使用默认的相等比较器). 命名空间: System.Linq 程序集: System.Core.dll 备注:实现此方法时使用了延迟执行. 它直接返回一个对象,该对象存储了执 ...
- Queryable.GroupBy<TSource, TKey> 方法 (IQueryable<TSource>, Expression<Func<TSource, TKey>>) 转
根据指定的键选择器函数对序列中的元素进行分组. 命名空间: System.Linq程序集: System.Core(在 System.Core.dll 中) 语法 C# C++ F# VB p ...
- 性能优化-列表类型转换(ConvertList<TSource, TResult>)
之前,在项目中看到过一段通用列表类型转换的代码,直接的实现便是用反射.大概看了下,它用在领域模型转DTO和SOA接口中契约实体的转换等等.首先,它使用了反射,其次,还是在循环中使用,便有了优化的想法. ...
- 列表类型转换(ConvertList<TSource, TResult>)
性能优化-列表类型转换(ConvertList<TSource, TResult>) 2013-12-16 16:55 by stevey, 426 阅读, 7 评论, 收藏, 编辑 之前 ...
随机推荐
- 试用windows Azure
试用windows Azure, 需要国外手机注册,信用卡注册. windows操作系统,只有2008R2,2012,2012R2可以选择,我选择XS最低档,然后选2012R2,欧洲数据中心,那个慢啊 ...
- iOS架构网址
http://casatwy.com/iosying-yong-jia-gou-tan-kai-pian.html
- 转:python webdriver API 之定位一组对象
webdriver 可以很方便的使用 find_element 方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,WebElement 接口同样提供了定位一组元素的方法 find_eleme ...
- codeforces 520 Pangram
http://codeforces.com/problemset/problem/520/A A. Pangram time limit per test 2 seconds memory limit ...
- fzu 2111 Min Number
http://acm.fzu.edu.cn/problem.php?pid=2111 Problem 2111 Min Number Accept: 572 Submit: 1106Tim ...
- [原创]java WEB学习笔记50:文件上传案例
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- requireJs和r.js压缩工具
上面release是执行命令 node r.js -o build.js 生成的,需要切换到目录require/tools下面,也就是 有r.js和build.js的目录,才能执行命令 代码目录如上: ...
- oracle热备份
1:热备份: SHUTDOWN IMMEDIATE; STARTUP MOUNT; alter database archivelog; --ALTER SYSTEM SET LOG_ARCHIVE_ ...
- 夺命雷公狗---DEDECMS----16dedecms取出首页今日更新
我们这次就要来取出我们的电影和电视剧以及综艺节目: 我们首先在我们受页面的模版文件中获取电影和电视剧的标签: 我们发现这里有一大堆,我只留一个即可: 然后我们到后台更新下首页的模版,看下是否只有一个模 ...
- 关闭微软对win10的推送
找到windows中安装的更新,卸载 KB3035583 这个更新即可