// 扩展IEnumerable<T>
static
class
ExternEnumerable {
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;
}
}
}
}
// 筛选去重调用
IEnumerable<User> ie = list;
// 先转换为IEnumerable接口类型
ie=ie.DistinctBy<User,
string
>(u=>u.DatasName+
"|"
+u.DatasType+
"|"
+u.DatasValue);