Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using the following dynamic Linq query to select distinct rows:

C#
var distinctValue = (from row in orgdt.AsEnumerable()
                                     select new { NAME = row.Field<string>("NAME")}).Distinct().ToList(); </string>


NAME is column of the table - in that there may be two rows like Emp and emp.
In list 2 names are displayed, but my requirement is that only one should be displayed. How can this be done?
Posted
Updated 17-Mar-20 21:48pm
v3

Distinct() can take an IEqualityComparer as an argument. Provide one that does a case-insensitive string comparison.
 
Share this answer
 
Use a where clause. As in,

from row in orgdt.AsEnumerable()
where row.Field("NAME") == "Emp"
 
Share this answer
 
You have to change to collation of that column (or the complete database) to "case insensitive" or perform the distinct operation in code.
 
Share this answer
 
Try this:

var distNames = Data.Select(s => s.Name.ToLower()).Distinct().ToList()
 
Share this answer
 
Comments
Richard Deeming 18-Mar-20 15:32pm    
A terrible idea. You're creating a new copy of every string in the list, which will be inefficient. And you're normalizing to lower-case, which means you could get hit by the "Turkish i" problem.

The Turkish İ Problem and Why You Should Care | You’ve Been Haacked[^]
CA1308: Normalize strings to uppercase - Visual Studio | Microsoft Docs[^]

Solution 3 is the correct solution: pass a case-insensitive equality comparer to the Distinct method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900