J Antoni

J Antoni

  • NA
  • 81
  • 35.7k

C# Linq to Group to a list if a Condition meets else do not

Sep 1 2017 7:06 AM
I have a list
My Requirement
I need a LINQ lambda query to Group to a list if a Condition meets else do not group.

i.e On a condition I want it to be grouped
else it should not be grouped

I have searched net - I get details on grouping on condition and I couldn't get and understand on how the remaining item should be included without grouping.

Some info found on net was for grouping Conditionally - but with that those items not meeting conditions do not include in the resultant list.

For Example
List<signal> = [{1,"a"},{40,""),{9,"a"},{52,"b"),{2,"b"},{99,""),{88,"b"}]
The expected resultant list is to be grouped by a,b but "" should not be grouped
  1. ResultantList = Group[0] ==> [{1,"a"}  
  2. {9,"a"}],  
  3. Group[1] ==>[ {52,"b"),  
  4. {2,"b"},  
  5. {88,"b"}] ,  
  6. // all other items which is "" should be included without groups  
  7. Group[3] [ {40,""}]  
  8. Group[4][ {99,""} ]  

Please share your solution for this.

What I have tried:
  1. var resultantList = sigList  
  2. .GroupBy(s => s.SignalGroup)  
  3. .Select(grp => grp.ToList())  
  4. //.Where(g => !g.Any(grp => grp.SignalGroup == ""))  
  5. .ToList();  
With the above as expected 
  1. Uncommenting Where clause groups only a and b==> All those items with empty value ( "" ) does not get included
  2. Commenting Where clause groups a,b and "" string to make a list with 3 groups(a,b and "").

Answers (2)