110,572
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
}//<-- End DistinctBy()
使用方法
var query = List.DistinctBy(p => new { p.xTorr, p.xCout });
public struct T
{
public int a;
}
//……
protected void Page_Load(object sender, EventArgs e)
{
//构造模拟输入列表
List<int> List_a=new List<int>{1,3,5,7,1,1,1,4,3,3,2,9,0,1,2,4,6,8};
List<T> myList = new List<T>();
T tempT;
foreach(int i in List_a)
{
tempT.a = i;
myList.Add(tempT);
}
//开始删除重复元素
for (int i = 0; i < myList.Count; i++)
{
for (int k = i + 1; k < myList.Count; k++)
{
if ( myList[i].a == myList[k].a)
{
myList.RemoveAt(k);
k--;
}
}
}
//输出去重后的列表
foreach(T t in myList)
{
Response.Write(t.a.ToString() + "<br />");
}
}
List<T> list = ...;
list = list.Distinct().ToList();