首页 > Windows开发 > 详细

C# List 对象去重

时间:2020-06-15 11:52:15      阅读:297      评论:0      收藏:0      [点我收藏+]

扩展类

 

  public static class ObjectExtensions
    {    
    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;
                }
            }
        }
    }

对象:

    /// <summary>
    /// 返回题目数据
    /// </summary>
    [Serializable]
    public class QuestionResult
    {
        /// <summary>
        /// 编号
        /// </summary>
        public long Id { get; set; }

        /// <summary>
        /// 题目内容
        /// </summary>
        public string Title { get; set; }
 
    }

  

用例:

list.DistinctBy(e => new { e.Id }).ToList();

 

C# List 对象去重

原文:https://www.cnblogs.com/wfpanskxin/p/13129627.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!