List去重的實現

来源:https://www.cnblogs.com/tangchun/archive/2018/11/02/9894566.html
-Advertisement-
Play Games

List<T> 當T為值類型的時候 去重比較簡單,當T為引用類型時,一般根據業務需要,根據T的中幾個屬性來確定是否重覆,從而去重。 查看System.Linq下的Enumerable存在一個去重方法 通過實現IEqualityComparer<T>比較器來實現對象的比較。 IEqualityComp ...


List<T> 當T為值類型的時候 去重比較簡單,當T為引用類型時,一般根據業務需要,根據T的中幾個屬性來確定是否重覆,從而去重。

查看System.Linq下的Enumerable存在一個去重方法

    /// <summary>Returns distinct elements from a sequence by using a specified <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare values.</summary>
        /// <param name="source">The sequence to remove duplicate elements from.</param>
        /// <param name="comparer">An <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare values.</param>
        /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
        /// <returns>An <see cref="T:System.Collections.Generic.IEnumerable`1" /> that contains distinct elements from the source sequence.</returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///         <paramref name="source" /> is <see langword="null" />.</exception>
        [__DynamicallyInvokable]
        public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
        {
            if (source == null)
            {
                throw Error.ArgumentNull("source");
            }
            return DistinctIterator(source, comparer);
        }

通過實現IEqualityComparer<T>比較器來實現對象的比較。

IEqualityComparer<T>的簡單實現,通過委托來比較對象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Extensions
{
    public delegate bool ComparerDelegate<T>(T x, T y);

    /// <summary>
    /// list比較
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ListCompare<T> : IEqualityComparer<T>
    {
        private ComparerDelegate<T> _comparer;

        public ListCompare(ComparerDelegate<T> @delegate)
        {
            this._comparer = @delegate;
        }

        public bool Equals(T x, T y)
        {
            if (ReferenceEquals(x, y))
            {
                return true;
            }
            if (_comparer != null)
            {
                return this._comparer(x, y);
            }
            else
            {
                return false;
            }
        }

        public int GetHashCode(T obj)
        {
            return obj.ToString().GetHashCode();
        }
    }
}

使用方法:

list= List.Distinct(new ListCompare<Path>
                ((x, y) => x.Latitude == y.Latitude && x.Longitude == y.Longitude)).ToList();

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一般情況我們會用枚舉類型來存儲一些狀態信息,而這些信息有時候需要在前端展示,所以需要展示中文註釋描述。 為了方便獲取這些信息,就封裝了一個枚舉擴展類。 上面代碼中的 ComboboxItemDto 類是來自 Abp 源碼,它主要用於提供前端下拉框的數據源。 好了,下麵來舉個慄子,這是一個訂單枚舉類 ...
  • 場景1 當使用 ShowDialog() 方式顯示視窗時,通過定義附加屬性的方式可實現在 ViewModel 中進行數據綁定(bool?)來控制子視窗的顯示和關閉 參考地址: "Getting “DialogResult can be set only after Window is created ...
  • C# 實現Excel(導出導入)非常實用的將數字轉換成字母 /// <summary> /// 需要轉換的行或者列 /// </summary> /// <param name="index"></param> /// <returns></returns> private static strin ...
  • 表類型(Table Type)實現百萬級別的數據一次性毫秒級別插入 ...
  • 本書全面介紹了微軟最新推出的編程語言C#.第1章介紹了學習C#必需的軟,硬體概念.第2-5章介紹了C#的基本語言元素與結構.第6-9章講解了進行C#編程的一些概念,包括類型,操作符,分支語句,迭代語句等.第10-11章討論了數組的使用.第12-18章詳細討論瞭如何運用C#進行面向對象編程.第19-2 ...
  • protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (string.IsNullOrEmpty(Request.Params["CarPicPath"]) == false) ... ...
  • <asp:BoundField DataField="BWFlag" HeaderText="黑/白名單" /> <asp:TemplateField HeaderText="車牌圖片" ItemStyle-Width="100px"> <ItemTemplate> <asp:LinkButton ...
  • protected void btnEdit_Click(object sender, EventArgs e) { if (txtCarPlateNumber.Text.Trim() == "") { ClientScript.RegisterStartupScript(GetType(), ..... ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...