var list = GetListFromSomeWhere();
var list2 = GetListFromSomeWhere();
list.AddRange(list2);
....
...
var distinctedList = list.DistinctBy(x => x.ID).ToList();
More LINQ
na google code
Albo jeśli nie chcesz korzystać z zewnętrznych bibliotek DLL z jakiegoś powodu, można użyć tego Distinct
przeciążeniem:
public static IEnumerable<TSource> Distinct<TSource>(
this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
Zastosowanie:
public class FooComparer : IEqualityComparer<Foo>
{
// Products are equal if their names and product numbers are equal.
public bool Equals(Foo x, Foo y)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
return x.ID == y.ID
}
}
list.Distinct(new FooComparer());
To działało idealnie tu jest moje wykonanie:. Lista uniqueRows = inputRows.GroupBy (x => x.Id) .Wybrać (x => x.First()) ToList (); –
Baxter
Cieszę się, że mogę Ci pomóc! Jedna uwaga: '' na twoim 'ToList()' jest zbędny. Powinieneś być w stanie zrobić '.ToList()' –
Masz rację, działa on tylko z funkcją ToList() zamiast z ToList() –
Baxter