如果条件满足,则C#linq分组到列表不分组 [英] C# linq to group to a list if a condition meets else do not group
问题描述
我有一个清单
我的要求
我需要一个LINQ lambda查询,如果一个条件遇到别人就不能分组到一个列表分组。
即条件我希望它被分组
否则它不应分组
我搜索了网 - 我得到了有关条件分组的详细信息,我无法了解如何在没有分组的情况下如何包含剩余项目。
在网上找到的一些信息是用于分组有条件的 - 但是那些不符合条件的项目不包括在结果列表中。
例如
列表< signal> = [{ 1 , a },{ 40 , ) ,{ 9 , a} ,{ 52 , b) ,{ 2 , b} ,{ 99 , ), { 88 , b}]
预期的结果列表将按a,b分组,但不应分组
ResultantList = Group [ 0 ] == > [{ 1 , a}
{ 9 , a}],
组[ 1 ] == > [{ 52 , b),
{ 2 , b},
{ 88 , b}],
// 所有其他项目应包括在内而不包括
组[ 3 ] [{ 40 , }]
组[ 4 ] [{ 99 , } ]
请分享您的解决方案。
我尝试过:
var resulList = sigList
.GroupBy(s = > s.SignalGroup)
。选择(grp = > ; grp.ToList())
// 。其中(g => !g.Any(grp => grp.SignalGroup ==))
.ToList();
如上所述按预期
1.取消注释Where子句组仅a和b ==>所有那些空值()的项目都不包含在内。
2.评论Where子句将a,b和字符串分组以制作一个包含3组的列表(a,b和)。
类似......
var resulList = sigList
.Where(x = > !string.IsNullOrEmpty(x.SignalGroup))
.GroupBy(s = > s.SignalGroup)
。选择(grp = > grp.ToList());
更像:
var resulList = sigList
。选择((x,i)=> new {Item = x,Index = i})
.GroupBy(a => string.IsNullOrEmpty(a.Item.SignalGroup)?a。 Index .ToString():a.Item.SignalGroup)
。选择(grp => grp。选择(a => a.Item).ToList());
让我把它分解:
你想让每个空字符串都在它自己的组中(不像第一个解决方案那样排除)。最简单的方法是按照独特的内容进行分组,例如索引。您可以使用选择扩展名映射索引:
。选择((x,i)= > new {Item = x,Index = i})
这将返回一个包含Item的匿名类索引作为参数
我们仍然希望其他项目由他们自己的SignalGroup分组。我希望这些不会是整数,所以你不会得到任何随机的错误分组。我们可以通过SignalGroup分组(如果有),或者如果没有则分组:
.GroupBy(a = > string .IsNullOrEmpty(a.Item.SignalGroup)?a.Index.ToString():a.Item.SignalGroup)
现在我们只需要从匿名类中选择原始项目。我们可以使用组中每个项目的选择扩展名来执行此操作:
。选择(grp = > grp.Select(a => a.Item).ToList());
这应该是你的点要求^ _ ^
更新
哦 - 我忘记了您可以指定要在groupby中分组的项目:
.GroupBy(a = > < span class =code-keyword> string .IsNullOrEmpty(a.Item.SignalGroup)?a.Index.ToString():a.Item.SignalGroup,a => a.Item)
然后您不需要在最后一步中选择项目:
。选择(grp = > grp.ToList());
无论哪种方式都很好
我得到了以下内容,它按预期工作
var resulList = s igList
.GroupBy(s = > s.SignalGroup == ? s.SignalID.ToString():s.SignalGroup)
.Select(grp = > grp.ToList())
.ToList( );
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
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 "").
Something like...
var resultantList = sigList .Where(x => !string.IsNullOrEmpty(x.SignalGroup)) .GroupBy(s => s.SignalGroup) .Select(grp => grp.ToList());
More like:
var resultantList = sigList .Select((x,i) => new {Item=x,Index=i}) .GroupBy(a => string.IsNullOrEmpty(a.Item.SignalGroup)?a.Index.ToString():a.Item.SignalGroup) .Select(grp => grp.Select(a=>a.Item).ToList());
Let me break it down:
You want to have each empty string in it's own group (not excluded like the first solution). The easiest way to do that is to group by something unique, such as the index. You can map the index using the Select extension:
.Select((x,i) => new {Item=x,Index=i})
This will return an anonymous class with Item and Index as Parameters
We still want the other items to group by their own SignalGroup. I hope that these won't be ints so you won't get any random mis-groupings. We can group by the SignalGroup if there is one, or the unique index if there isn't:
.GroupBy(a => string.IsNullOrEmpty(a.Item.SignalGroup)?a.Index.ToString():a.Item.SignalGroup)
Now we just need to select out the original item from the anonymous class. We can do this using the select extension for each item within the group:
.Select(grp => grp.Select(a=>a.Item).ToList());
That should be spot on what you asked for ^_^
UPDATE
Ooh - I forgot that you can specify the items to be grouped in the groupby:
.GroupBy(a => string.IsNullOrEmpty(a.Item.SignalGroup)?a.Index.ToString():a.Item.SignalGroup, a=>a.Item)
Then you don't need to select out the items in the last step:
.Select(grp => grp.ToList());
Either way is fine
I got the below and it works as expected
var resultantList = sigList .GroupBy(s => s.SignalGroup == "" ? s.SignalID.ToString() : s.SignalGroup) .Select(grp => grp.ToList()) .ToList();
这篇关于如果条件满足,则C#linq分组到列表不分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!