Linq:按条件和最大大小n拆分列表 [英] Linq: Split list by condition and max size of n

查看:88
本文介绍了Linq:按条件和最大大小n拆分列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要转换数组:

["a", "b", "b", "a", "b", "a", "b"]

["a", "a", "b", "b", "a", "b", "b"]["b", "b", "a", "a", "b", "b", "a"]

我想以一种特殊条件匹配的方式对数组进行分组.就我而言,当item == 'a'item == 'b'时.我想将这些小组分为2个小组.我目前对如何以一种优雅的方式感到困惑.

I want to group the array in a way where a special condition matches. In my case when item == 'a' or item == 'b'. And these groups I want to chunck into groups of 2. I'm currently a bit confused how to do it the elegant way.

任何人都可以帮忙吗?

以下内容可能会更清楚:

Maybe the following makes it more clear:

我喜欢先将数组分为"a"项和"b"项:

I like to group the array into 'a' and 'b'-items first like so:

一个小组:

["a","a","a"]

b组:

["b","b","b","b"]

然后我想将其分成2组:

then I want to chunk this into groups of 2:

一个小组:

["a","a"]
["a"]

b组:

["b","b"]
["b","b"]

现在我想将它们合并在一起以得到结果:

And now I want to merge them together to get the result:

["a","a","b","b","a","b","b"]

(每组总是2个合并在一起)

(always 2 of each group merged together)

推荐答案

首先,您需要GroupBy您的数据.假设对象是string,但无论如何,只要没有其他条件,分组条件就会改变.

First you need to GroupBy your data. Let assume the object are string but it's irrelevant anyway it's your grouping condition that will change if you have anything other than that.

要执行此操作,您将需要MoreLinq或简单地包含Batch扩展名,该扩展名将对剩余部分进行2和1分组".可以在此处

For this to work you will need MoreLinq or simply include the Batch extension which does the "group by 2 and 1 for left overs". Details can be found here

请注意,可以将Batch(2)更改为所需的任何内容.如果您输入Batch(5)并 您有7个元素,它将分为2组,一个包含5个元素,另一个包含2个元素

note that the Batch(2) can be changed to whatever you need. If you put Batch(5) and you have 7 elements it will make 2 groups, one with 5 elements and one of 2 elements

 // be my data, 4 x a, 3 x b, 1 x c, 2 x d, As a list for easier linQ
 var data = new[] { "a", "a", "c", "b", "a", "b", "b", "d", "d", "a" }.ToList();

 // group by our condition. Here it's the value so very simple
 var groupedData = data.GroupBy(o => o).ToList();

 // transform all groups into a custom List of List of List so they are grouped by 2 internally
 // each list level represent Grouping -> Groups of 2 (or 1) -> element
 var groupedDataBy2 = groupedData.Select(grp => grp.ToList().AsEnumerable().Batch(2).ToList()).ToList();

 // find the group with the maximum amount of groups or 2 (and 1)
 var maxCount = groupedDataBy2.Max(grp => grp.Count());

 // will contain our final objects listed
 var finalObjects = new List<string>();

 // loop on the count we figured out and try add each group one by one
 for (int i = 0; i < maxCount; i++)
 {
     // try each group
     foreach (var group in groupedDataBy2)
     {
         // add the correct index to our final list only if the current group has enough to fill
         if (i < group.Count)
         {
             // add the data to our final list
             finalObjects.AddRange(group[i]);
         }
     }
 }

 // result here is : a,a,c,b,b,d,d,a,a,b
 var results = string.Join(",", finalObjects);

这篇关于Linq:按条件和最大大小n拆分列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆