Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
2
Reply
C# Linq to Group to a list if a Condition meets else do not
J Antoni
Sep 1 2017 7:06 AM
253
Reply
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
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,
""
} ]
Please share your solution for this.
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 "").
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
2
)
Next Recommended Forum
how can call a notification method like fb in asp.net
MVC Database fetching error