当前位置:网站首页>C list set object de duplication LINQ de duplication with time de duplication
C list set object de duplication LINQ de duplication with time de duplication
2022-07-19 04:35:00 【po123op123】
demand : Only one piece of data can appear at the same time

In actual projects, many implement this interface through specific implementation classes , By rewriting Equals and HashCode Compare attribute values to achieve the goal of de duplication , Because for each class, the corresponding comparator must be implemented , So it's not universal , On the contrary, the best way is to use the above methods , In fact, we can use this comparison interface to achieve a general solution , The reason why we have to implement a comparator for every class is , We put attribute comparison inside the interface of the class , What if we put attribute comparison on the periphery , At this time, the general solution is reached , So how do we achieve it , Through delegation , The essence of implementing this interface is nothing more than comparison HashCode, And then through Equals Compare the values , When comparing HashCode when , We force its value to be a constant ( such as 0), When rewriting Equals Method we call the delegate , as follows
1. Create a new class , Yes IEnumerable Expand
public static class Extensions
{
public static IEnumerable<T> Distinct<T>(
this IEnumerable<T> source, Func<T, T, bool> comparer)
where T : class
=> source.Distinct(new DynamicEqualityComparer<T>(comparer));
private sealed class DynamicEqualityComparer<T> : IEqualityComparer<T>
where T : class
{
private readonly Func<T, T, bool> _func;
public DynamicEqualityComparer(Func<T, T, bool> func)
{
_func = func;
}
public bool Equals(T x, T y) => _func(x, y);
public int GetHashCode(T obj) => 0;
}
}
2. call Take the time as hours, minutes and seconds to remove the duplicate And sort
var item1 = item.Distinct((a, b) => a.Item3.ToString("T") == b.Item3.ToString("T")).ToList().OrderBy(i=>i.Item3);After processing

边栏推荐
- 基于STM32的SG90舵机实验含代码(HAL库)
- 删除文件 unable to find or create trash dirctory
- Oracle操作openldap的权限问题
- 数据分析报告这样写,才算真正读懂了数据
- ospf防环
- leetcode209. 长度最小的子数组
- [wechat applet] super easy to understand conditional rendering and list rendering
- 物业小区高校水电抄表充值管理系统
- Autojs learning - realize blissful pure land
- Software testing - use cases
猜你喜欢

智能硬件语音控制的时频图分类挑战赛2.0(思路以及结果,目前top5)
![[wechat applet] super easy to understand conditional rendering and list rendering](/img/02/c45ed1f4682a3436a6ef67003ca9d4.jpg)
[wechat applet] super easy to understand conditional rendering and list rendering

物业小区高校水电抄表充值管理系统

Impersonate the server for requests

Wechat e-book reading of applet completion works (7) Interim inspection report

OSPF comprehensive experiment

WildFly:如何从位于另一个应用程序中的 EJB 调用 EJB

Gin framework principle

使用kaggle跑李宏毅机器学习作业

MySql 一行变多行(根据特定符号分割)
随机推荐
MySQL中判断和向下取整的使用场景和用法
mysql中的视图、触发器和存储过程
Autojs learning-2048 small game automation
golang反转reverse切片slice代码示例
Fetch request - simple record
[Unity] 交互之双击
TiDB学习笔记【初阶】
[Vuforia] 图像识别的简单逻辑
数据分析报告这样写,才算真正读懂了数据
OpenLDAP自定义schema
Week 2022/7/16
万文多图之Word高效插入参考文献
ASP. Net1==visual studio create asp net demo
Overview of Baidu map technology, and application development of basic API and webapi
Autojs learning - realize blissful pure land
MySql 一行变多行(根据特定符号分割)
OSPF basic optimization
[wechat applet] super easy to understand conditional rendering and list rendering
Architecture and application of secure multiparty computing
Tidb learning notes [preliminary]