当前位置:网站首页>C#中IEqualityComparer接口的实现
C#中IEqualityComparer接口的实现
2022-06-29 09:26:00 【zlbcdn】
在用linq写代码时,可能会用到去重的功能。若只是string类型的还好,则可通过如下的代码进行去重:
var testList = (from r in realresultList select r).Distinct<string>().toList<string>();
但若realresultList中包含的是类,则需要对类进行处理。那就用到了IEqualityComparer接口。
具体实现如下:
1、先定义自己的model
public class QueryCommentModel
{
public string NurseStation {
get; set; }
public string DeptID {
get; set; }
public string DeptName {
get; set; }
public string PharmacistID {
get; set; }
public string PharmacistName {
get; set; }
public string OrderCount {
get; set; }
public string RightCommentStatus {
get; set; }
public string ErrorCommentStatus {
get; set; }
}
2、实现接口
public class QueryCommentModelComparer : IEqualityComparer<QueryCommentModel>
{
//实现具体的比较逻辑(与实际业务相关)
public bool Equals(QueryCommentModel x, QueryCommentModel y)
{
bool checkFlag = true;
if (Object.ReferenceEquals(x, y))
{
checkFlag = true;
}
else if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
{
checkFlag = false;
}
else
{
if (x.NurseStation == y.NurseStation && x.DeptID == y.DeptID && x.PharmacistID == y.PharmacistID)
{
checkFlag = true;
}
else
{
checkFlag = false;
}
}
return checkFlag;
}
//实现获取哈希值
public int GetHashCode(QueryCommentModel model)
{
if (Object.ReferenceEquals(model, null)) return 0;
int hashNurse = model.NurseStation.GetHashCode();
int hashDeptID = model.DeptID.GetHashCode();
int hashPharmacist = model.PharmacistID.GetHashCode();
return hashNurse ^ hashDeptID ^ hashPharmacist;
}
}
3、在业务代码中实现
//通过比较器,实现类的去重
var testList = (from r in realresultList select r).Distinct<QueryCommentModel>(new QueryCommentModelComparer()).ToList<QueryCommentModel>();
边栏推荐
- Power strings [KMP cycle section]
- Text of the basic component of the shutter
- TLAB of JVM
- HDU 6778 car (group enumeration -- > shape pressure DP)
- L2-031 go deep into the tiger's den (25 points)
- Arc view and arc viewpager
- The collapsing "2.3 * 10 = 22" produced by multiplying float and int
- Codeforces Round #652 (Div. 2)
- 2019.10.30学习总结
- 子串分值-超详细版——最后的编程挑战
猜你喜欢

Slide the custom control to close the activity control

Six dimensional space BFS

2021 team programming ladder competition - Simulation Competition

Alternative implementation of Scrollview pull-down header amplification

Pipeline details of IPC (interprocess communication)

GridView of basic component of shutter

自定义控件之侧滑关闭 Activity 控件

Codeforces Round #645 (Div. 2)

qgis制图

Rikka with Cake(线段树+线段树)
随机推荐
HDU 6778 car (group enumeration -- > shape pressure DP)
Rikka with Cake(线段树+线段树)
This open source project is super wow, and handwritten photos are generated Online
2019.11.17训练总结
2019-11-10训练总结
L2-026 小字辈 (25 分)
Recyclerview sticky (suspended) head
Codeforces Round #652 (Div. 2)
Flutter 基础组件之 Text
2019.11.13 training summary
這個開源項目超哇塞,手寫照片在線生成
520 钻石争霸赛 2021
2019.10.6训练总结
2019.11.13训练总结
十六制计数器和流水灯
To 3 -- the last programming challenge
2019.10.20训练总结
Rikka with cake (segment tree + segment tree)
The collapsing "2.3 * 10 = 22" produced by multiplying float and int
Application of keil5 integrated development environment for single chip microcomputer