網站首頁 編程語言 正文
C# EF去除重復列DistinctBy
在網上看了LinQ有DistinctBy方法,實際在用的時候并沒有找到,后來參照了該網站才發現寫的是拓展方法
https://www.jb51.net/article/273355.htm
1.添加一個擴展方法
? ? public static class DistinctByClass
? ? {
? ? ? ? public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
? ? ? ? {
? ? ? ? ? ? HashSet<TKey> seenKeys = new HashSet<TKey>();
? ? ? ? ? ? foreach (TSource element in source)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (seenKeys.Add(keySelector(element)))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? yield return element;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
2.使用方法如下(針對ID,和Name進行Distinct)
var query = people.DistinctBy(p => new { p.Id, p.Name });
3.若僅僅針對ID進行distinct:
var query = people.DistinctBy(p => p.Id);
C#集合用Distinct去掉重復的元素,IEqualityComparer<T>原理
?json測試數據
[
?? ?{
?? ??? ?"Type": "ROLETYPE_USER",
?? ??? ?"Value": "9846cdac-ae21-4be4-a284-50158dd19606"
?? ?},
?? ?{
?? ??? ?"Type": "ROLETYPE_USER",
?? ??? ?"Value": "4f6665fc-b0b9-4865-bf12-eff06810efa3"
?? ?},
?? ?{
?? ??? ?"Type": "ROLETYPE_USER",
?? ??? ?"Value": "ca4dbf59-d248-4538-9fe8-62e1cafcde6c"
?? ?},
?? ?{
?? ??? ?"Type": "ROLETYPE_USER",
?? ??? ?"Value": "7c4852c2-b2c2-4688-92a3-3dd1b00bacf8"
?? ?},
?? ?{
?? ??? ?"Type": "ROLETYPE_USER",
?? ??? ?"Value": "982a2f9d-a079-4613-825f-c2ef9801eb3e"
?? ?},
?? ?{
?? ??? ?"Type": "ROLETYPE_USER",
?? ??? ?"Value": "ca4dbf59-d248-4538-9fe8-62e1cafcde6c"
?? ?},
?? ?{
?? ??? ?"Type": "ROLETYPE_USER",
?? ??? ?"Value": "ca4dbf59-d248-4538-9fe8-62e1cafcde6c"
?? ?},
?? ?{
?? ??? ?"Type": "ROLETYPE_ROLE",
?? ??? ?"Value": "76f62bf8-bf24-48c1-b70e-7628ff08c3fb"
?? ?},
?? ?{
?? ??? ?"Type": "ROLETYPE_USER",
?? ??? ?"Value": "982a2f9d-a079-4613-825f-c2ef9801eb3e"
?? ?}
]
實體類型
? ? public class TempSetting
? ? {
? ? ? ?public string Type { get; set; }
? ? ? ? public Guid Value { get; set; }
? ? }
創建比較類,繼承自IEqualityComparer<TSource>,一般要選擇特征值字段來設置hashCode值返回才有效果,或者直接設置哈希值返回1(這樣每次都會調用Equals方法,但這樣性能低)。
IEqualityComparer比較對象是否相等是優先調用GetHashCode()比較哈希值是否相等,不相等的就不會調用Equals方法,如果哈希值相等的才會調用調用Equals方法再比較是否相等最終確認,所以 GetHashCode方法里面要設置特征值字段來返回hashcode,這樣速度才快,性能高
?public class CompareSetting : IEqualityComparer<TempSetting>
? ? {
? ? ? ? public bool Equals(TempSetting x, TempSetting y)
? ? ? ? {
? ? ? ? ? ? return x.Value == y.Value;
? ? ? ? }
?
? ? ? ? public int GetHashCode(TempSetting obj)
? ? ? ? {
? ? ? ? ? ? if (obj == null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return 0;
? ? ? ? ? ? }
? ? ? ? ? ? //return obj.GetHashCode();//比較不出來重復的
? ? ? ? ? ? //string km = obj.Type + obj.Value;//比較重復值成功
? ? ? ? ? ? //return km.GetHashCode();
? ? ? ? ? ? //GetHashCode推薦選擇自己看中的特征值字段來進行比較,否則有時候可能比較不成功
? ? ? ? ? ? return obj.Value.GetHashCode();
? ? ? ? }
? ? }
?
//測試比較
? ? ? ? string funs = "[{\"Type\":\"ROLETYPE_USER\",\"Value\":\"9846cdac-ae21-4be4-a284-50158dd19606\"},{\"Type\":\"ROLETYPE_USER\",\"Value\":\"4f6665fc-b0b9-4865-bf12-eff06810efa3\"},{\"Type\":\"ROLETYPE_USER\",\"Value\":\"ca4dbf59-d248-4538-9fe8-62e1cafcde6c\"},{\"Type\":\"ROLETYPE_USER\",\"Value\":\"7c4852c2-b2c2-4688-92a3-3dd1b00bacf8\"},{\"Type\":\"ROLETYPE_USER\",\"Value\":\"982a2f9d-a079-4613-825f-c2ef9801eb3e\"},{\"Type\":\"ROLETYPE_USER\",\"Value\":\"ca4dbf59-d248-4538-9fe8-62e1cafcde6c\"},{\"Type\":\"ROLETYPE_USER\",\"Value\":\"ca4dbf59-d248-4538-9fe8-62e1cafcde6c\"},{\"Type\":\"ROLETYPE_ROLE\",\"Value\":\"76f62bf8-bf24-48c1-b70e-7628ff08c3fb\"},{\"Type\":\"ROLETYPE_USER\",\"Value\":\"982a2f9d-a079-4613-825f-c2ef9801eb3e\"}]";
? ? ? ? ? ? List<TempSetting> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TempSetting>>(funs);
? ? ? ? ? ? var list2 = list.Distinct(new CompareSetting());
總結
原文鏈接:https://blog.csdn.net/ying456baby/article/details/81075336
相關推薦
- 2022-12-04 Golang如何快速構建一個CLI小工具詳解_Golang
- 2022-02-17 Centos 安裝docker 警告:正在等候 事務 鎖定 arb/rpm/.rpm.lock 正在
- 2022-09-25 Django 使用定時任務
- 2022-05-02 Shell命令中的特殊替換、模式匹配替換、字符串提取和替換的實現_linux shell
- 2022-10-21 C++?ffmpeg硬件解碼的實現方法_C 語言
- 2022-11-21 C++運行時類型識別與轉換實現方法_C 語言
- 2023-07-28 dialog 對話框垂直居中
- 2022-12-29 python解決循環依賴的問題分析_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支