Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case insensitive group on multiple columns

Is there anyway to do a LINQ2SQL query doing something similar to this:

var result = source.GroupBy(a => new { a.Column1, a.Column2 });

or

var result = from s in source
             group s by new { s.Column1, s.Column2 } into c
             select new { Column1 = c.Key.Column1, Column2 = c.Key.Column2 };

but with ignoring the case of the contents of the grouped columns?

like image 684
dav_i Avatar asked Apr 24 '13 10:04

dav_i


1 Answers

You can pass StringComparer.InvariantCultureIgnoreCase to the GroupBy extension method.

var result = source.GroupBy(a => new { a.Column1, a.Column2 }, 
                StringComparer.InvariantCultureIgnoreCase);

Or you can use ToUpperInvariant on each field as suggested by Hamlet Hakobyan on comment. I recommend ToUpperInvariant or ToUpper rather than ToLower or ToLowerInvariant because it is optimized for programmatic comparison purpose.

like image 157
NaveenBhat Avatar answered Sep 23 '22 05:09

NaveenBhat