记录:linq自定义去重DistinctBy

创建一个cs文件,放进去即可 

public static IEnumerable DistinctBy(this IEnumerable source, Func keySelector)
        {
            HashSet seenKeys = new HashSet();
            foreach (TSource element in source)
            {
                if (seenKeys.Add(keySelector(element)))
                {
                    yield return element;
                }
            }
        }

使用:

list.DistinctBy(t => new { t.ID, t.Name});

 

你可能感兴趣的:(c#)