linq 之 Distinct的使用
public class Product
{
public string Name { get;set;}
public int Code { get; set; }
}
class ProductComparet : IEqualityComparer<Product>
{
public bool Equals(Product x, Product y)
{
if (object.ReferenceEquals(x,y))
{
return true;
}
if (object.ReferenceEquals(x,null)||object.ReferenceEquals(y,null))
{
return false;
}
return x.Code == y.Code && x.Name == y.Name;
} public int GetHashCode(Product product)
{
if (object.ReferenceEquals(product, null))
{
return ;
}
int hashProductName = product.Name == null ? : product.Name.GetHashCode(); int hashProductCode = product.Code.GetHashCode();
return hashProductName ^ hashProductCode; }
}
static void Main(string[] args)
{ Product[] products = { new Product { Name = "apple", Code = 9 },
new Product { Name = "orange", Code = 4 },
new Product { Name = "apple", Code = 9 },
new Product { Name = "lemon", Code = 12 } }; IEnumerable<Product> noduplicates = products.Distinct(new ProductComparet());
foreach (var item in noduplicates)
{
Console.WriteLine(item.Name+" "+item.Code);
}
}
linq 之 Distinct的使用的更多相关文章
- Linq的Distinct方法的扩展
原文地址:如何很好的使用Linq的Distinct方法 Person1: Id=1, Name="Test1" Person2: Id=1, Name="Test1&qu ...
- Core源码(二) Linq的Distinct扩展
先贴源码地址 https://github.com/dotnet/corefx/tree/master/src/System.Linq/src .NET CORE很大一个好处就是代码的开源,你可以详细 ...
- Linq使用Distinct删除重复数据时如何指定所要依据的成员属性zz
最近项目中在用Linq Distinct想要将重复的资料去除时,发现它跟Any之类的方法有点不太一样,不能很直觉的在呼叫时直接带入重复数据判断的处理逻辑,所以当我们要用某个成员属性做重复数据的判断时, ...
- Linq之Distinct详解
前天在做批量数据导入新增时,要对数据进行有效性判断,其中还要去除重复,如果没出现linq的话可能会新声明一个临时对象集合,然后遍历原始数据判断把符合条件的数据添加到临时集合中,这在有了linq之后显得 ...
- Linq Enumerable.Distinct方法去重
Enumerable.Distinct 方法 是常用的LINQ扩展方法,属于System.Linq的Enumerable方法,可用于去除数组.集合中的重复元素,还可以自定义去重的规则. 有两个重载方法 ...
- 如何很好的使用Linq的Distinct方法
Person1: Id=1, Name="Test1" Person2: Id=1, Name="Test1" Person3: Id=2, Name=&quo ...
- Linq的Distinct太不给力了[转]
假设我们有一个类:Product public class Product { public string Id { get; set; } public string Name { get; set ...
- 扩展Linq的Distinct方法动态根据条件进行筛选
声明为了方便自己查看所以引用 原文地址:http://www.cnblogs.com/A_ming/archive/2013/05/24/3097062.html Person1: Id=1, Nam ...
- Linq 中 Distinct 方法扩展
原文链接 http://www.cnblogs.com/A_ming/archive/2013/05/24/3097062.html public static class LinqEx { publ ...
随机推荐
- 北大OJ 1001题
题目:输入一序列的正实数和幂次(正整数)对,然后打印结果(具体的比这个精细) 这道题是关于大数计算的(大数求幂),从开始建立思路,到写代码.调式到最后被AC以及最终的优化,总共用了差不多一天的时间.开 ...
- The hierarchy of the type NsRedisConnectionFactory is inconsistent
The hierarchy of the type is inconsistent 解释为:层次结构的类型不一致 由于我在eclipse里建了两个JAVA PROJECT项目,分别是A projiec ...
- Angularjs做的一个小页面
<!DOCTYPE html><html lang="en" ng-app="todolist"> <head> <m ...
- php csv导出
/** * 下载csv * @param unknown $orders_id * @param unknown $orders_date_start * @param unknown $orders ...
- Java--笔记(3)
21.当涉及到继承时,按照如下顺序执行: (1).执行父类的静态代码块 (2).执行子类的静态代码块 (3).执行父类的构造代码块 (4).执行父类的构造函数 (5).执行子类的构造代码块 (6).执 ...
- 区间DP lightoj 1422
t个样例 n n个数字 从 1->n 穿衣服 脱了就不能再用 ,可以套 问最少几件衣服 #include<stdio.h> #include<string.h> # ...
- css-画三角箭头
.arrow { width:; height:; content: ""; border: solid 10px #7c7; display: block; border-top ...
- fiddler使用教程
转载地址:写得很不错的fildder教程 http://kb.cnblogs.com/page/130367/ Fiddler的基本介绍 Fiddler的官方网站: www.fiddler2.c ...
- Python3 捕捉异常
可以通过try/except语句来实现捕获异常,如下: bpython version 0.15 on top of Python 3.5.1+ /usr/bin/python3 >>&g ...
- ubuntu apt-get update失败 解决方法
ubuntu apt-get update失败 1.出现错误:E:Could not get lock /var/lib/apt/lists/lock - open (11: Resource tem ...