【共享】根据属性名称去除集合重复项

sunzsh 2010-01-09 09:28:02
利用linq写个方法。根据属性名去除 集合重复项。欢迎大家探讨

public IList<T> FilterRepeat<T>(IList<T> t, string attName)
{
if (t.Count <= 0)
return t;

#region 查找attName所在的位置
int index = -1;
for (int i = 0; i < t[0].GetType().GetProperties().Count(); i++)
{
if (t[0].GetType().GetProperties()[i].Name == attName)
{
index = i;
break;
}
}
#endregion

if (index == -1)
return t;

IList<T> result = new List<T>();
var sr = from s in t group s by s.GetType().GetProperties()[index].GetValue(s, null);
foreach (var group in sr)
result.Add(group.ToList()[0]);
return result;
}

...全文
123 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunzsh 2010-01-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 twistmost 的回复:]
去除重复项可以用 Enumerable.Distinct
[/Quote]
试过了,很不错。
twistmost 2010-01-10
  • 打赏
  • 举报
回复
去除重复项可以用 Enumerable.Distinct

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧