Given:
var s = (from p in operatorList
select p.ID, p.Name,p.Phone)
How would I return the Distinct records based only on the ID?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could write an
IEqualityComparerthat compares theIDvalues and pass it into the overloaded Queryable.Distinct method, but since this is LINQ to SQL it won’t be supported on the database. You would have to add a call to theAsEnumerablemethod to get it working, but this isn’t recommended for large amounts of data because you would be bringing the data down to the client side. If you decide to go that route you will end up with a query similar to:The other option, which makes the database do the work, is to group by
IDand take the first item in each group: