欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# EF去除重復(fù)列DistinctBy方式

 更新時(shí)間:2023年01月24日 13:37:50   作者:---清心寡欲---  
這篇文章主要介紹了C# EF去除重復(fù)列DistinctBy方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

C# EF去除重復(fù)列DistinctBy

在網(wǎng)上看了LinQ有DistinctBy方法,實(shí)際在用的時(shí)候并沒(méi)有找到,后來(lái)參照了該網(wǎng)站才發(fā)現(xiàn)寫(xiě)的是拓展方法

http://www.dbjr.com.cn/article/273355.htm

1.添加一個(gè)擴(kuò)展方法

? ? 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.使用方法如下(針對(duì)ID,和Name進(jìn)行Distinct)

var query = people.DistinctBy(p => new { p.Id, p.Name });

3.若僅僅針對(duì)ID進(jìn)行distinct:

var query = people.DistinctBy(p => p.Id);

C#集合用Distinct去掉重復(fù)的元素,IEqualityComparer<T>原理

 json測(cè)試數(shù)據(jù)

[
?? ?{
?? ??? ?"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"
?? ?}
]

實(shí)體類(lèi)型

? ? public class TempSetting
? ? {
? ? ? ?public string Type { get; set; }
? ? ? ? public Guid Value { get; set; }
? ? }

創(chuàng)建比較類(lèi),繼承自IEqualityComparer<TSource>,一般要選擇特征值字段來(lái)設(shè)置hashCode值返回才有效果,或者直接設(shè)置哈希值返回1(這樣每次都會(huì)調(diào)用Equals方法,但這樣性能低)。

IEqualityComparer比較對(duì)象是否相等是優(yōu)先調(diào)用GetHashCode()比較哈希值是否相等,不相等的就不會(huì)調(diào)用Equals方法,如果哈希值相等的才會(huì)調(diào)用調(diào)用Equals方法再比較是否相等最終確認(rèn),所以 GetHashCode方法里面要設(shè)置特征值字段來(lái)返回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();//比較不出來(lái)重復(fù)的
? ? ? ? ? ? //string km = obj.Type + obj.Value;//比較重復(fù)值成功
? ? ? ? ? ? //return km.GetHashCode();
? ? ? ? ? ? //GetHashCode推薦選擇自己看中的特征值字段來(lái)進(jìn)行比較,否則有時(shí)候可能比較不成功
? ? ? ? ? ? return obj.Value.GetHashCode();
? ? ? ? }
? ? }
?
//測(cè)試比較
? ? ? ? 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());

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論