日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#?EF去除重復列DistinctBy方式_C#教程

作者:---清心寡欲--- ? 更新時間: 2023-03-20 編程語言

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

欄目分類
最近更新