var list = GetListFromSomeWhere();
var list2 = GetListFromSomeWhere();
list.AddRange(list2);
....
...
var distinctedList = list.DistinctBy(x => x.ID).ToList();
More LINQ
a google code
O se non si desidera utilizzare DLL esterne per qualche motivo, è possibile utilizzare questo Distinct
overload:
public static IEnumerable<TSource> Distinct<TSource>(
this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
Usage:
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());
questo ha funzionato perfettamente qui è la mia realizzazione:. Lista uniqueRows = inputRows.GroupBy (x => x.Id) .Select (x => x.First()) ToList (); –
Baxter
Felice di aiutare! Una nota: il '' sul tuo 'ToList()' è ridondante. Dovresti essere in grado di fare '.ToList()' –
Hai ragione funziona con solo ToList() invece di ToList() –
Baxter