TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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
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 "").
Reply
Answers (
2
)
how can call a notification method like fb in asp.net
MVC Database fetching error