亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

完美解決c# distinct不好用的問(wèn)題

 更新時(shí)間:2020年12月07日 11:13:18   作者:萌橙  
這篇文章主要介紹了完美解決c# distinct不好用的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

當(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)文章

最新評(píng)論