当前位置:网站首页>Implementation of iequalitycomparer interface in C #
Implementation of iequalitycomparer interface in C #
2022-06-29 10:26:00 【zlbcdn】
In use linq When writing code, , The function of weight removal may be used . If only string The type is OK , You can use the following code to de duplicate :
var testList = (from r in realresultList select r).Distinct<string>().toList<string>();
But if realresultList Contains classes , You need to process the class . That's where we use IEqualityComparer Interface .
The specific implementation is as follows :
1、 First define your own 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、 Implementation interface
public class QueryCommentModelComparer : IEqualityComparer<QueryCommentModel>
{
// Implement specific comparison logic ( Related to the actual business )
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;
}
// Get the hash value
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、 Implement... In business code
// Through the comparator , De duplication of implementation classes
var testList = (from r in realresultList select r).Distinct<QueryCommentModel>(new QueryCommentModelComparer()).ToList<QueryCommentModel>();
边栏推荐
- Codeforces Round #641 Div2
- Is it safe to open a stock account with the QR code given by the manager of a securities firm? I want to open an account
- 51nod1277 maximum value in string [KMP]
- manacher
- September 25, 2020 noncopyable of boost library for singleton mode
- 单片机集成开发环境Keil5的使用
- L2-026 small generation (25 points)
- Win32Exception (0x80004005): 组策略阻止了这个程序。要获取详细信息,请与系统管理员联系。
- The stones game
- 六度空间 bfs
猜你喜欢

在VMware workstation中安装WMware ESXi 6.5.0并进行配置

使用Rancher搭建Kubernetes集群

函数指针、函数指针数组、计算器+转移表等归纳总结

Rikka with cake (segment tree + segment tree)

Alibaba cloud firewall configuration, multiple settings (iptables and firewall)

Virtual machine port scanning

1146 topological order (25 points)

PGP在加密技术中的应用

Nacos环境隔离

View CSDN blog rankings
随机推荐
Serpentine filling number
Codeforces Round #659 (Div. 2)
PGP在加密技术中的应用
September 23, 2020 left and right values reference std:: move()
L1-009 N个数求和 (20 分)
Time varying and non time varying
Alibaba cloud firewall configuration, multiple settings (iptables and firewall)
单片机集成开发环境Keil5的使用
L2-3 is this a binary search tree- The explanation is wonderful
通过Win32API调用另一界面的按钮
To 3 -- the last programming challenge
The Stones Game【取石子博弈 & 思维】
1021 deep root (25 points)
基辅周边的凄美废墟——切尔诺贝利的安全前往指南!
JNI.h说明
EDA and VHDL question bank
云主机端口扫描
The stones game
Common usage of LINQ in C #
Text of the basic component of the shutter