C# list<T>去重

文章目录

C# list去重

值类型去重

List

object is int
csharp 复制代码
//object is int
   List<object> ointList = new List<object>();
   ointList.Add(1);
   ointList.Add(1);
   ointList.Add(2);
   ointList = ointList.Distinct().ToList();
object is decimal
csharp 复制代码
//object is decimal
            List<object> odecimalList = new List<object>();
            odecimalList.Add(1.25);
            odecimalList.Add(1.25);
            odecimalList.Add(2.25);
            odecimalList = odecimalList.Distinct().ToList();
object is char
csharp 复制代码
            //object is char
            List<object> ocharList = new List<object>();
            ocharList.Add('a');
            ocharList.Add('a');
            ocharList.Add('b');
            ocharList = ocharList.Distinct().ToList();
object is bool
csharp 复制代码
            //object is bool
            List<object> oboolList = new List<object>();
            oboolList.Add(true);
            oboolList.Add(false);
            oboolList.Add(true);
            oboolList = oboolList.Distinct().ToList();
object is string
csharp 复制代码
            //object is string
            List<object> osList = new List<object>();
            osList.Add("1");
            osList.Add("1");
            osList.Add("2");
            osList = osList.Distinct().ToList();

List

csharp 复制代码
            List<int> intList = new List<int>();
            intList.Add(1111);
            intList.Add(1111);
            intList.Add(2222);
            intList = intList.Distinct().ToList();

List

csharp 复制代码
            List<string> idList = new List<string>();
            idList.Add("1111");
            idList.Add("1111");
            idList.Add("2222");  
            idList = idList.Distinct().ToList();

引用类型去重

csharp 复制代码
private class NetworkCtrl
        {

            #region <属性>

            /// <summary>
            /// 网控对象ID
            /// </summary>
            public string NetCtrlId { get; set; }

            /// <summary>
            /// 业务对象ID
            /// </summary>
            public string InterID { get; set; }

            /// <summary>
            /// 业务操作
            /// </summary>
            public string OperationDesc { get; set; }
            #endregion <属性>

        }
csharp 复制代码
List<NetworkCtrl> ncList = new List<NetworkCtrl>();
            ncList.Add(new NetworkCtrl
            {
                InterID = "100148",
                NetCtrlId = "9cc2c432-56a9-b38d-11ee-794e3a9853e2",
                OperationDesc = "售后单-SH231103001-下推其它出库单"
            });
            ncList.Add(new NetworkCtrl
            {
                InterID = "100149",
                NetCtrlId = "9cc2c432-56a9-b38d-11ee-794e3a9853e2",
                OperationDesc = "售后单-SH231103002-下推其它出库单"
            });
            ncList.Add(new NetworkCtrl
            {
                InterID = "100148",
                NetCtrlId = "9cc2c432-56a9-b38d-11ee-794e3a9853e2",
                OperationDesc = "售后单-SH231103001-下推其它出库单"
            });
            var ncList1 = ncList.Distinct().ToList();
            var ncList2 = ncList.GroupBy(x => x.InterID).Select(y => y.First()).ToList();

集合里有三条记录,其中两条重复。

使用Distinct后,还有三条,说明distinct失败

原因是,引用类型即使属性一样,引用地址是不一样的。

只能用别的方式去避免。

相关推荐
心平气和量大福大34 分钟前
C#-WPF-UserControl-生命周期(加载 退出)
开发语言·c#·wpf
sunywz1 小时前
【c#】 Web Deploy一键发布,IIS部署全流程
开发语言·前端·c#
一只小菜鸡..1 小时前
南京大学 操作系统 (JYY) 学习笔记:导论与历史的轮回 (OS Introduction)
笔记·学习
法雅特吉他1 小时前
吉他选购预算分档:1000/1500/2000元各买什么配置
经验分享·新媒体运营·产品运营·用户运营·教育电商
xqqxqxxq1 小时前
LeetCode Hot100 双指针专项题解笔记
笔记·算法·leetcode
懿路向前2 小时前
【HarmonyOS学习笔记】2026-07-24 | textProcessing 实体识别与踩坑实录
笔记·学习·边缘计算·harmonyos
春生野草2 小时前
C基础--八大排序(个人笔记)
c语言·笔记·排序算法
bug嘛我经常写3 小时前
如何批量删除word文档中存在的无用样式
经验分享·python·word
晓晨的博客3 小时前
word表格其中一行填写内容后跳到下一页
经验分享
万联WANFLOW3 小时前
外贸独立站:服务器放海外国内后台卡,放国内海外客户打不开——这道两头难怎么破?
运维·网络·数据库·经验分享