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 ...
随机推荐
- 【jQuery EasyUI系列】使用属性介绍
1.ValidateBox The validatebox is designed to validate the form input fields.If users enter invalid v ...
- 37-wc 简明笔记
显示行数.单词数和字节数 wc [options] [file-list] 参数 file-list是wc分析的一个或多个文件的路径名列表.如果省略file-list,wc就从标准输入中读取输入 选项 ...
- python环境搭建-Pycharm 调整字体大小
- mysql-删除日志文件命令详解
装载 在mysql中会生大量的如mysq-bin.000001这类日志文件了,这些都是二进制文件了,如果我们是普通的日志没有进行主从配置就可以直接使用reset master进行删除了这个方法很简单, ...
- Bootstrap表单布局样式
1.并排和下拉选项 <form class="form-horizontal" role="form"> <fieldset> < ...
- JAVA对文件类型的校验
通常,在WEB系统中,上传文件时都需要做文件的类型校验,大致有如下几种方法: 1. 通过后缀名,如exe,jpg,bmp,rar,zip等等. 2. 通过读取文件,获取文件的Content-type来 ...
- Value和Object的区别
在使用NSMutableDictionary的时候经常会使用setValue forKey与setObject forKey,他们经常是可以交互使用的,代码中经常每一种的使用都有. 1,先看看setV ...
- iOS正则表达式
//包含数字和字母的密码长度6-16位 -(BOOL) validatePassword:(NSString *)password { //密码正则表达式 NSString *passwordRege ...
- Python 从零学起(纯基础) 笔记(一)
作者身份:初学Python,菜鸟 ================================================= 1. 主提示符和次提示符 >>> 主提示符 ...
- Hive 的 map join
学习自 http://blog.csdn.net/xqy1522/article/details/6699740 1. Map Join 的使用场景: 关联操作中有一张表非常小 不等值的链接操作 2. ...