C#根据对象的指定字段去除重复值
PersonInfo类:
public class PersonInfo
{
public int Index;
public string Name; public override string ToString()
{
return string.Format("Index:{0}, Name:{1}", Index, Name);
}
}
PersonInfoCompareByName比较类:
public class PersonInfoCompareByName : IEqualityComparer<PersonInfo>
{
public bool Equals(PersonInfo x, PersonInfo y)
{
if (x != null && y != null && x.Name == y.Name)
return true; return false;
} public int GetHashCode(PersonInfo obj)
{
if (obj == null)
{
return ;
}
else
{
return obj.Name.GetHashCode();
}
}
}
下面的代码是新建一个列表,然后去除重复项:
private void Button_Click(object sender, RoutedEventArgs e)
{
List<PersonInfo> PersonList = new List<PersonInfo>();
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
var info = new PersonInfo();
info.Index = j;
info.Name = "Name" + i;
PersonList.Add(info);
}
} var items = PersonList.Distinct(new PersonInfoCompareByName());
foreach (var item in items)
{
Console.WriteLine(item.ToString());
}
}
C#根据对象的指定字段去除重复值的更多相关文章
- ruby pluck用法,可以快速从数据库获取 对象的 指定字段的集合数组
可以快速从数据库获取 对象的 指定字段的集合数组 比如有一个users表,要等到user的id数组: select id from users where age > 20; 要实现在如上sql ...
- JAVA中List对象去除重复值的方法
JAVA中List对象去除重复值,大致分为两种情况,一种是List<String>.List<Integer>这类,直接根据List中的值进行去重,另一种是List<Us ...
- 利用Entity Framework修改指定字段中的值
利用Entity Framework修改指定字段中的值一般我们编辑某些模型的时候会用到类似这样的代码: [HttpPost] public ActionResult Edit(Article mode ...
- 扩展lamda表达中distinct按照字段去除重复
首先,我们定义一个Student类来测试. public class Student { public int ID { get; set; } public string Name { get; s ...
- MySQL获取指定字段不重复的记录
关键词:DISTINCT 1.比如数据库一组数据查询如下,返回店铺下所有的区域id 2.SQL统计返回指定字段 district 不重复的 记录id,SQL如下 SELECT DISTINCT(dis ...
- 二维数组去除重复值和array_unique函数
今天遇到了一个问题,就是从数据库中去除的数组为一个二维数组,现在就是想将二位数组进行去重,但是在php中,对于一个一维数组,我们可以直接使用php的系统函数array_unique,但是这个函数不能对 ...
- php二维数组去除重复值
<?php //二维数组 $test["aa"] = array("id"=>"17","name"=> ...
- mysql 面试题 查询出表中某字段的重复值
users 表中有 两个字段 id 和 name 表数据大概如下: id name 1 AAA 2 BBB 3 CCC 4 AAA 请写查 ...
- js数组中如何去除重复值?
在日常开发中,我们可能会遇到将一个数组中里面的重复值去除,那么,我就将我自己所学习到的几种方法分享出来 去除数组重复值方法: 1,利用indexOf()方法去除 思路:创建一个新数组,然后循环要去重的 ...
随机推荐
- [Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda's Converge
When doing comparisons inside of functions, you end of relying heavily on the argument passed into t ...
- java完美equals方法代码段
public boolean equals(Object otherObject) { if(this == otherObject) { // 检測this与otherObject是否引用同一个对象 ...
- JavaScript调用ATL COM(二)
作者:朱金灿 来源:http://blog.csdn.net/clever101 在上篇文章中介绍了如何在JS中调用ATL COM: JS调用ATL COM中的C++接口的做法 现在我们可以把它嵌入到 ...
- Erlang类型及函数声明规格
http://erlangdisplay.iteye.com/blog/404570 Erlang类型及函数声明规格 Author: Mail: Date: Copyright: litaocheng ...
- WPF 中那些可跨线程访问的 DispatcherObject(WPF Free Threaded Dispatcher Object)
原文 WPF 中那些可跨线程访问的 DispatcherObject(WPF Free Threaded Dispatcher Object) 众所周知的,WPF 中多数对象都继承自 Dispatch ...
- Obtaining Directory Change Notifications(微软的例子,使用FindFirstChangeNotification,FindNextChangeNotification,FindCloseChangeNotification API函数)
An application can monitor the contents of a directory and its subdirectories by using change notifi ...
- Java 中override、overload、overwrite区别,以及与多态的关系【转】
因为早期的翻译导致了override和overwrite的解释及理解混乱,需要重新梳理这几个词及相关内容. 转自:http://blog.csdn.net/lzhang007/article/deta ...
- C#中的SMTP配置Outlook.Com SMTP主机
如果你想以编程方式使用 Outlook.com或Gmail帐户作为 SMTP主机 发送电子邮件,也有为了得到这一切工作的几件事情要注意. 使用基本的System.Net.Mail库, ...
- 在webapi中使用swagger
1 在webapi项目下安装swagger,包名 Swashbuckle.AspNetCore 2 在webapi的startup.cs文件中添加swagger服务 /// <summary&g ...
- 2-2 Consul注册注销流程
铺垫,创建健康检查方法,Consul服务器隔一段时间请求一下webapi里的一个方法,如果这个方法没有问题,则证明这个webapi还在正常工作,这个webapi提供的服务就存在.如果方法没有返回,或者 ...