完美解決c# distinct不好用的問(wèn)題
當(dāng)一個(gè)結(jié)合中想根據(jù)某一個(gè)字段做去重方法時(shí)使用以下代碼
IQueryable 繼承自IEnumerable
先舉例:
#region linq to object
List<People> peopleList = new List<People>();
peopleList.Add(new People { UserName = "zzl", Email = "1" });
peopleList.Add(new People { UserName = "zzl", Email = "1" });
peopleList.Add(new People { UserName = "lr", Email = "2" });
peopleList.Add(new People { UserName = "lr", Email = "2" });
Console.WriteLine("用擴(kuò)展方法可以過(guò)濾某個(gè)字段,然后把當(dāng)前實(shí)體輸出");
peopleList.DistinctBy(i => new { i.UserName }).ToList().ForEach(i => Console.WriteLine(i.UserName + i.Email));
Console.WriteLine("默認(rèn)方法,集合中有多個(gè)字段,當(dāng)所有字段發(fā)生重復(fù)時(shí),distinct生效,這與SQLSERVER相同");
peopleList.Select(i => new { UserName = i.UserName, Email = i.Email }).OrderByDescending(k => k.Email).Distinct().ToList().ForEach(i => Console.WriteLine(i.UserName + i.Email));
Console.WriteLine("集合中有一個(gè)字段,將這個(gè)字段重復(fù)的過(guò)濾,并輸出這個(gè)字段");
peopleList.Select(i => new { i.UserName }).Distinct().ToList().ForEach(i => Console.WriteLine(i.UserName));
#endregion
該擴(kuò)展方法貼出:
public static class EnumerableExtensions
{
public static IEnumerable<TSource> DistinctBy<TSource, Tkey>(this IEnumerable<TSource> source, Func<TSource, Tkey> keySelector)
{
HashSet<Tkey> hashSet = new HashSet<Tkey>();
foreach (TSource item in source)
{
if (hashSet.Add(keySelector(item)))
{
yield return item;
}
}
}
}
補(bǔ)充知識(shí):c# – .Distinct()調(diào)用不過(guò)濾
我正在嘗試使用AsEnumerable將Entity Framework DbContext查詢拉入IEnumerable< SelectListItem>.這將用作填充視圖中下拉列表的模型屬性.
但是,盡管調(diào)用了Distinct(),但每個(gè)查詢都會(huì)返回重復(fù)的條目.
public IEnumerable<SelectListItem> StateCodeList { get; set; }
public IEnumerable<SelectListItem> DivCodeList { get; set; }
DivCodeList =
db.MarketingLookup.AsEnumerable().OrderBy(x => x.Division).Distinct().Select(x => new SelectListItem
{
Text = x.Division,
Value = x.Division
}).ToList();
StateCodeList =
db.MarketingLookup.AsEnumerable().OrderBy(x => x.State).Distinct().Select(x => new SelectListItem
{
Text = x.State,
Value = x.State
}).ToList();
為了使Distinct生效,如果類型是自定義類型,則序列必須包含實(shí)現(xiàn)IEquatable接口的類型的對(duì)象.
正如here所述:
Distinct returns distinct elements from a sequence by using the
default equality comparer to compare values.
一個(gè)解決方法,為了避免上述情況,因?yàn)槲铱梢缘贸鼋Y(jié)論,你不需要整個(gè)對(duì)象而不是它的一個(gè)屬性,就是將序列的每個(gè)元素投影到Division,然后創(chuàng)建OrderBy并調(diào)用Distinct :
var divisions = db.MarketingLookup.AsEnumerable()
.Select(ml=>ml.Division)
.OrderBy(division=>division)
.Distinct()
.Select(division => new SelectListItem
{
Text = division,
Value = division
}).ToList();
有關(guān)此問(wèn)題的進(jìn)一步文檔,請(qǐng)查看here.
以上這篇完美解決c# distinct不好用的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)單鏈表(線性表)完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)單鏈表(線性表)的方法,結(jié)合完整實(shí)例形式分析了單鏈表的原理、實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-06-06
C#實(shí)現(xiàn)winform自動(dòng)關(guān)閉MessageBox對(duì)話框的方法
這篇文章主要介紹了C#實(shí)現(xiàn)winform自動(dòng)關(guān)閉MessageBox對(duì)話框的方法,實(shí)例分析了C#中MessageBox對(duì)話框的相關(guān)操作技巧,需要的朋友可以參考下2015-04-04
C#中LINQ to Objects查詢的實(shí)現(xiàn)
LINQ to Objects是LINQ技術(shù)在C#中的一種應(yīng)用,它專門(mén)用于對(duì)內(nèi)存中的對(duì)象集合進(jìn)行查詢和操作,本文就詳細(xì)的介紹C#中LINQ to Objects查詢的實(shí)現(xiàn),感興趣的可以了解一下2023-08-08
C#函數(shù)式編程中的遞歸調(diào)用之尾遞歸詳解
這篇文章主要介紹了C#函數(shù)式編程中的遞歸調(diào)用詳解,本文講解了什么是尾遞歸、尾遞歸的多種方式、尾遞歸的代碼實(shí)例等內(nèi)容,需要的朋友可以參考下2015-01-01
深入C#中使用SqlDbType.Xml類型參數(shù)的使用詳解
本篇文章是對(duì)在C#中使用SqlDbType.Xml類型參數(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C# WinForm開(kāi)發(fā)中使用XML配置文件實(shí)例
這篇文章主要介紹了C# WinForm開(kāi)發(fā)中使用XML配置文件實(shí)例,本文詳細(xì)講解了如何使用一個(gè)XML文件作為WinForm的配置文件,需要的朋友可以參考下2014-08-08

