ASP.NET Lambda去重方法

来源:互联网 发布:大连 知乎 编辑:程序博客网 时间:2024/06/05 14:35


using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace BusinessLogic{    public static class LambdaHelper    {        // var query = people.DistinctBy(p => p.Id);        // var query = people.DistinctBy(p => new { p.Id, p.Name });        /// <summary>        /// 去重复数据        /// </summary>        /// <typeparam name="TSource"></typeparam>        /// <typeparam name="TKey"></typeparam>        /// <param name="source"></param>        /// <param name="keySelector">用于去重的表达式,单个字段如:var query = people.DistinctBy(p => p.Id);多个字段如:var query = people.DistinctBy(p => new { p.Id, p.Name });</param>        /// <returns></returns>        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;                }            }        }    }}


0 0