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 = [{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
ResultantList = Group[0] ==> [{1,"a"}
{9,"a"}],
Group[1] ==>[ {52,"b"),
{2,"b"},
{88,"b"}] ,
// all other items which is "" should be included without groups
Group[3] [ {40,""}]
Group[4][ {99,""} ]
What I have tried
var resultantList = sigList
.GroupBy(s => s.SignalGroup)
.Select(grp => grp.ToList())
//.Where(g => !g.Any(grp => grp.SignalGroup == ""))
.ToList();
With the above as expected
Uncommenting Where clause groups only a and b==> All those items with empty value ( "" ) does not get included
Commenting Where clause groups a,b and "" string to make a list with 3 groups(a,b and "").