网页学习体会

  • 首页
  • 个人博客
您的位置: 首页  >  技术问答  >  C#中 list 怎么去除重复的数据 ?

C#中 list 怎么去除重复的数据 ?
分类: 技术问答 • 2022-03-04 10:34:56

C#中 list<object> 怎么去除重复的数据 ?

问题描述:

C#中 list 怎么去除重复的数据 ?object可能是类等复杂类型。谢谢!

答

用distinct 方法

之前的程序我用表达式代替反射,给你写了一下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Q703207
{
    static class LinqExt
    {
        public class DGroupBy<T> : IGrouping<object[], T>
        {
            private List<T> _innerlist = new List<T>();

            private object[] _key;

            public DGroupBy(object[] key) { _key = key; }

            public object[] Key
            {
                get { return _key; }
            }

            public void Add(T value)
            {
                _innerlist.Add(value);
            }

            public IEnumerator<T> GetEnumerator()
            {
                return this._innerlist.GetEnumerator();
            }

            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return this._innerlist.GetEnumerator();
            }
        }

        public static IEnumerable<IGrouping<object[], T>> DynamicGroupBy<T>(this IEnumerable<T> data, string[] keys)
        {
            List<DGroupBy<T>> list = new List<DGroupBy<T>>();

            var exps = keys.Select(x =>
            {
                var para = Expression.Parameter(typeof(T), "p");
                Expression<Func<T, object>> GetProp = Expression.Lambda<Func<T, object>>(
                    Expression.MakeMemberAccess(para, typeof(T).GetProperty(x)), para) as Expression<Func<T, object>>;
                return GetProp.Compile();
            }).ToArray();

            foreach (var item in data.Select(x => new {
                k = exps.Select(y => y(x)).ToArray(),
                v = x
            }))
            {
                DGroupBy<T> existing = list.SingleOrDefault(x => x.Key.Zip(item.k, (a, b) => a.Equals(b)).All(y => y));
                if (existing == null)
                {
                    existing = new DGroupBy<T>(item.k);
                    list.Add(existing);
                }
                existing.Add(item.v);
            }
            return list;
        }
    }

    class User
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string City { get; set; }
        public override string ToString()
        {
            return string.Format("{0},{1},{2},{3}", ID, Name, Age ,City);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<User> users = new List<User>()
            {
                new User() { ID = 1, Age = 20, City = "BJ", Name = "A" },
                new User() { ID = 2, Age = 20, City = "BJ", Name = "B" },
                new User() { ID = 3, Age = 20, City = "BJ", Name = "C" },
                new User() { ID = 4, Age = 20, City = "SH", Name = "D" },
                new User() { ID = 5, Age = 30, City = "SH", Name = "E" },
                new User() { ID = 6, Age = 30, City = "SH", Name = "F" },
                new User() { ID = 7, Age = 30, City = "CD", Name = "G" },
                new User() { ID = 8, Age = 30, City = "CD", Name = "H" },
                new User() { ID = 9, Age = 30, City = "HK", Name = "I" },
                new User() { ID = 10, Age = 30, City = "HK", Name = "J" },
            };
            var query = users.DynamicGroupBy(new string[] { "City" });
            foreach (var item in query)
            {
                Console.WriteLine("Key: {0}", string.Join(",", item.Key.Select(x => x.ToString())));
                foreach (var item1 in item)
                    Console.WriteLine("\t" + item1);
            }
        }
    }
}

相关推荐

  • C#如何对List中的Object进行排序
  • Java中List集合去除重复数据的方法
  • 去除List集合中的重复值、去除数组的重复值
  • C# 对List中的Object进行排序
  • 去除List集合中的重复元素? 如果没有Set集合,List集合是如何去除重复元素的(字符串类型,自定义类型)
  • 可变长度的集合List 怎么存储到数据库中
  • 在C#中,怎么连接已加密的Sqlite数据库
  • 关于数据库某字段中是以逗号分割的数据,去除重复有关问题
  • C# list 掏出重复的数据
  • C# 怎么以存在的Excel模板导出所需要的表格(保持模板格式并需要添加数据库数据),数据从Access数据库导出;最好能在程序中添加打印预览功能(分可以加)
  • c#中list要怎么取值对应起来
  • Python3之RabbitMQ
    网站免责声明 网站地图 最新文章 用户隐私 版权申明
本站所有数据收集于网络,如果侵犯到您的权益,请联系网站进行下架处理。   

Copyright © 2018-2021   Powered By 网页学习体会    备案号:   粤ICP备20002247号