C# list<T>去重

文章目录

  • C# list去重
    • 值类型去重
      • List
        • object is int
        • object is decimal
        • object is char
        • object is bool
        • object is string
      • List
      • List
      • 引用类型去重
      • C# list去重

        值类型去重

        List

        object is int
        //object is int
           List<object> ointList = new List<object>();
           ointList.Add(1);
           ointList.Add(1);
           ointList.Add(2);
           ointList = ointList.Distinct().ToList();
        

        C# list<T>去重_第1张图片

        object is decimal
        //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();
        

        C# list<T>去重_第2张图片

        object is char
                    //object is char
                    List<object> ocharList = new List<object>();
                    ocharList.Add('a');
                    ocharList.Add('a');
                    ocharList.Add('b');
                    ocharList = ocharList.Distinct().ToList();
        

        C# list<T>去重_第3张图片

        object is bool
                    //object is bool
                    List<object> oboolList = new List<object>();
                    oboolList.Add(true);
                    oboolList.Add(false);
                    oboolList.Add(true);
                    oboolList = oboolList.Distinct().ToList();
        

        C# list<T>去重_第4张图片

        object is string
                    //object is string
                    List<object> osList = new List<object>();
                    osList.Add("1");
                    osList.Add("1");
                    osList.Add("2");
                    osList = osList.Distinct().ToList();
        

        C# list<T>去重_第5张图片

        List

                    List<int> intList = new List<int>();
                    intList.Add(1111);
                    intList.Add(1111);
                    intList.Add(2222);
                    intList = intList.Distinct().ToList();
        

        C# list<T>去重_第6张图片

        List

                    List<string> idList = new List<string>();
                    idList.Add("1111");
                    idList.Add("1111");
                    idList.Add("2222");  
                    idList = idList.Distinct().ToList();
        

        C# list<T>去重_第7张图片

        引用类型去重

        private class NetworkCtrl
                {
        
                    #region <属性>
        
                    /// 
                    /// 网控对象ID
                    /// 
                    public string NetCtrlId { get; set; }
        
                    /// 
                    /// 业务对象ID
                    /// 
                    public string InterID { get; set; }
        
                    /// 
                    /// 业务操作
                    /// 
                    public string OperationDesc { get; set; }
                    #endregion <属性>
        
                }
        
        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();
        

        集合里有三条记录,其中两条重复。
        C# list<T>去重_第8张图片
        使用Distinct后,还有三条,说明distinct失败
        C# list<T>去重_第9张图片
        原因是,引用类型即使属性一样,引用地址是不一样的。
        只能用别的方式去避免。
        C# list<T>去重_第10张图片

        你可能感兴趣的:(C#,工作笔记,C#基础,c#,list,经验分享,笔记)