最好的办法是添加一个扩展方法:原来的dstinct()就可以用DistinctBy()替换
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;
}
}
}
Entity.Meet = itgms.Where(g => g.FileNames != null).
Select(h => new { h.Id, h.Url, h.U_Name, h, h.FileNames }).GroupBy(q => q.FileNames).
Select(w => new FileAppth()
{
FileNameType = w.Key.Name,
Id = w.Key.Id.ToString(),
list = w.Select(h => new FileTypes()
{
Id = h.Id.ToString(),
Name = h.U_Name,
AttachPath = h.U_Name.IndexOf(".") == -1 ? h.U_Name + ".doc" : h.U_Name,
OrginalAttachName = h.Url
}).ToList().DistinctBy(x => x.Name).ToList()
}).ToList();