Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,


I have a linq query which is used to group by, where the count is >1.

Linq query is like this:
SQL
var Serialnumberentered = from a in serialNumbercheck
                                        group a by new { a } into g
                                        where g.Count() > 1
                                        select new
                                        {
                                            g.Key
                                        };

when i entered empty values in "serialNumbercheck", the above query is taking the count of empty values also.can any one help me how can i ignore the empty values.
Posted
Comments
VJ Reddy 17-May-12 12:15pm    
Thank you for accepting the solution :)
inayat basha 28-May-12 6:32am    
Hi VJ,
from the query u have provided how can i check the duplicate for upper case and lower case. example i have "test" and "TEST" as the data it was not taking as duplicate. can you plz suggest me

1 solution

Introduce a where clause to remove the empty strings as follows

C#
var Serialnumberentered = from a in serialNumbercheck
//For .NET 2.0 and 3.5      where a == null ? false : a.Trim() != string.Empty  
//or                        where (a ?? "").Trim()!= string.Empty
                            where !string.IsNullOrWhiteSpace(a)
                            group a by new { a } into g
                            where g.Count() > 1
                            select new
                            {
                                g.Key
                            };
 
Share this answer
 
v4
Comments
Pete O'Hanlon 17-May-12 11:59am    
Gets my 5.
VJ Reddy 17-May-12 12:14pm    
Thank you very much.
Maciej Los 17-May-12 12:51pm    
Good answer, my 5!
VJ Reddy 17-May-12 13:00pm    
Thank you, losmac :)
member60 17-May-12 23:51pm    
my 5!

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