1. 在專案裡新增 IEnumerable 擴充類別即可
public static class IEnumerableExtensions { public static IEnumerable<TSource> Distinct<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) { HashSet<TKey> seenKeys = new HashSet<TKey>(); foreach (TSource element in source) { var elementValue = keySelector(element); if (seenKeys.Add(elementValue)) { yield return element; } } } }
2. 加完就可直接叫用(以會員ID來當條件篩選為例)
var distinctDatas = SourceDatas.Distinct(member => member.ID);
參考來源:C#使用Distinct去除重複資料時指定依據的成員屬性