I\'m trying to use Linq to return a list of ids given a list of objects where the id is a property. I\'d like to be able to do this without looping through each object and p
When taking Distinct we have to cast into IEnumerable too. If list is model means, need to write code like this
IEnumerable<T> ids = list.Select(x => x).Distinct();
Use the Distinct operator:
var idList = yourList.Select(x=> x.ID).Distinct();
int[] numbers = {1,2,3,4,5,3,6,4,7,8,9,1,0 };
var nonRepeats = (from n in numbers select n).Distinct();
foreach (var d in nonRepeats)
{
Response.Write(d);
}
OUTPUT
1234567890
Using straight Linq, with the Distinct()
extension:
var idList = (from x in yourList select x.ID).Distinct();
IEnumerable<int> ids = list.Select(x=>x.ID).Distinct();