最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Linq利用Distinct去除重復(fù)項(xiàng)問題(可自己指定)

 更新時(shí)間:2023年01月24日 13:31:05   作者:MrCui.  
這篇文章主要介紹了Linq利用Distinct去除重復(fù)項(xiàng)問題(可自己指定),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Linq利用Distinct去除重復(fù)項(xiàng)

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

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;
? ? ? ? }
? ? }
}

使用方法如下(針對(duì)ID,和Name進(jìn)行Distinct)

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

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

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

Linq利用Except去除重復(fù)數(shù)據(jù)并返回唯一數(shù)據(jù)(IEqualityComparer擴(kuò)展)

前段時(shí)間做一個(gè)項(xiàng)目就是定時(shí)下載節(jié)目列表進(jìn)行對(duì)文件時(shí)間和名字進(jìn)行新舊對(duì)比進(jìn)行去重復(fù),眾所周知,我們?cè)贚inq中去重復(fù)數(shù)據(jù)都用Distinct()做。

但如果想多個(gè)條件進(jìn)行對(duì)比去除重復(fù)數(shù)據(jù),我們應(yīng)該怎么辦呢?

請(qǐng)看下文,利用Except (通過使用默認(rèn)的相等比較器對(duì)值進(jìn)行比較,生成兩個(gè)序列的差集。)

  //
        // 摘要:
        //     通過使用默認(rèn)的相等比較器對(duì)值進(jìn)行比較,生成兩個(gè)序列的差集。
        //
        // 參數(shù):
        //   first:
        //     System.Collections.Generic.IEnumerable`1 也不是在其元素 second 將返回。
        //
        //   second:
        //     System.Collections.Generic.IEnumerable`1 同時(shí)出現(xiàn)在第一個(gè)序列的元素將導(dǎo)致從返回的序列中移除這些元素。
        //
        // 類型參數(shù):
        //   TSource:
        //     輸入序列中的元素的類型。
        //
        // 返回結(jié)果:
        //     包含這兩個(gè)序列的元素的差集的序列。
        //
        // 異常:
        //   T:System.ArgumentNullException:
        //     first 或 second 為 null。
        public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);

示例:

public class ChannelTvListInfo
    {
        public string TVName { get; set; } //節(jié)目列表名字
        public string LastWriteTime { get; set; }//最后編輯文件時(shí)間
    }
private List<ChannelTvListInfo> lstNewTvInfo, lstOldTvInfo = new List<ChannelTvListInfo>();
		private void button3_Click(object sender, EventArgs e)
		{       //通過下載后與定時(shí)下載的目錄文件進(jìn)行名字及最后編輯文件的時(shí)間進(jìn)行對(duì)比更新
			lstNewTvInfo = listFTPFiles("60.208.140.xxx", "", "");
			DirectoryInfo TheFolder = new DirectoryInfo(@"D:\ChannelTvXML\");
			foreach (FileInfo NextFile in TheFolder.GetFileSystemInfos())
			{
				lstOldTvInfo.Add(new ChannelTvListInfo { TVName = NextFile.Name, LastWriteTime = 
NextFile.LastWriteTime.ToString("yyyy/MM/dd hh:mm tt") });
			}
			
		}
		public List<ChannelTvListInfo> listFTPFiles(string FTPAddress, string username, string password)
		{
			List<ChannelTvListInfo> listinfo = new List<ChannelTvListInfo>();
			using (FtpConnection ftp = new FtpConnection(FTPAddress, username, password))
			{
				ftp.Open();
				ftp.Login();
				foreach (var file in ftp.GetFiles("/"))
				{
					listinfo.Add(new ChannelTvListInfo
					{
						TVName = file.Name,
						LastWriteTime = Convert.ToDateTime(file.LastWriteTime).ToString("yyyy/MM/dd hh:mm tt")
					});
				}
				ftp.Dispose();
				ftp.Close();
			}
			return listinfo;
		}

效果圖

1:自動(dòng)從FTP目錄下載下來的xml 節(jié)目列表:

2:從上一個(gè)時(shí)間段自動(dòng)下來的存放的目錄獲取文件列表

或者新舊List列表中的 差異節(jié)目列表方法:

var result = lstNewTvInfo.Except(lstOldTvInfo.Where(x=>x.TVName.Contains("四川")), new ProductComparer()).ToList();

以下示例顯示如何實(shí)現(xiàn)可在Distinct <TSource>方法中使用的等式比較器。

	public class ProductComparer : IEqualityComparer<ChannelTvListInfo>
	{
		// Products are equal if their names and product numbers are equal.
		public bool Equals(ChannelTvListInfo x, ChannelTvListInfo y)
		{
 
			//Check whether the compared objects reference the same data.
			if (Object.ReferenceEquals(x, y)) return true;
 
			//Check whether any of the compared objects is null.
			if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
				return false;
 
			//Check whether the products' properties are equal.
			return x.TVName == y.TVName && x.LastWriteTime == y.LastWriteTime;
		}
 
		// If Equals() returns true for a pair of objects 
		// then GetHashCode() must return the same value for these objects.
 
		public int GetHashCode(ChannelTvListInfo product)
		{
			//Check whether the object is null
			if (Object.ReferenceEquals(product, null)) return 0;
 
			//Get hash code for the Name field if it is not null.
			int hashProductName = product.TVName == null ? 0 : product.TVName.GetHashCode();
 
			//Get hash code for the Code field.
			int hashProductCode = product.LastWriteTime.GetHashCode();
 
			//Calculate the hash code for the product.
			return hashProductName ^ hashProductCode;
		}
 
	}

最終返回結(jié)果就是有差異的

總結(jié)

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

相關(guān)文章

  • c#之OpenFileDialog解讀(打開文件對(duì)話框)

    c#之OpenFileDialog解讀(打開文件對(duì)話框)

    這篇文章主要介紹了c#之OpenFileDialog(打開文件對(duì)話框),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • C#華氏溫度和攝氏溫度相互轉(zhuǎn)換的方法

    C#華氏溫度和攝氏溫度相互轉(zhuǎn)換的方法

    這篇文章主要介紹了C#華氏溫度和攝氏溫度相互轉(zhuǎn)換的方法,涉及C#數(shù)學(xué)運(yùn)算的相關(guān)技巧,非常簡單實(shí)用,需要的朋友可以參考下
    2015-07-07
  • C#利用WebClient實(shí)現(xiàn)兩種方式下載文件

    C#利用WebClient實(shí)現(xiàn)兩種方式下載文件

    本篇文章主要介紹了C#利用WebClient 兩種方式下載文件,詳細(xì)的介紹了兩種方式,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
    2017-02-02
  • C# Page用于各頁面繼承功能實(shí)例

    C# Page用于各頁面繼承功能實(shí)例

    這篇文章主要介紹了C# Page用于各頁面繼承功能實(shí)例,包含了常見的頁面視圖、數(shù)據(jù)緩存、數(shù)據(jù)庫操作等技巧,需要的朋友可以參考下
    2014-10-10
  • Unity3D實(shí)現(xiàn)虛擬按鈕控制人物移動(dòng)效果

    Unity3D實(shí)現(xiàn)虛擬按鈕控制人物移動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)虛擬按鈕控制人物移動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • 使用C#實(shí)現(xiàn)將CSV文件內(nèi)容裝配成對(duì)象列表

    使用C#實(shí)現(xiàn)將CSV文件內(nèi)容裝配成對(duì)象列表

    這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)將CSV文件內(nèi)容裝配成對(duì)象列表,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12
  • C# 中的var關(guān)鍵字詳細(xì)介紹

    C# 中的var關(guān)鍵字詳細(xì)介紹

    這篇文章主要介紹了C# 中的var關(guān)鍵字詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • C#快速排序算法實(shí)例分析

    C#快速排序算法實(shí)例分析

    這篇文章主要介紹了C#快速排序算法,實(shí)例分析了C#排序方法的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • C# Newtonsoft.Json用法詳解

    C# Newtonsoft.Json用法詳解

    本文主要介紹了C# Newtonsoft.Json用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • c# datetime 格式化大全

    c# datetime 格式化大全

    這篇文章主要介紹了c# datetime 格式化大全,有需要的朋友可以參考一下
    2014-01-01

最新評(píng)論

高雄市| 大丰市| 马边| 海林市| 洛隆县| 保德县| 沾化县| 华容县| 古丈县| 铜鼓县| 昆明市| 醴陵市| 江阴市| 和平区| 大厂| 乐业县| 淄博市| 安远县| 白沙| 罗山县| 虎林市| 大姚县| 汉寿县| 开鲁县| 绥宁县| 潞城市| 金沙县| 昌邑市| 南郑县| 抚州市| 丹凤县| 大同市| 新干县| 米脂县| 右玉县| 平原县| 陆丰市| 安多县| 华池县| 泗水县| 北宁市|