将一个 IEnumerable 拆分为多个 IEnumerable

我是 linq 新手,我需要根据指示器将 Couple(string text, bool Indicator) 类型的 IEnumerable 拆分为多个 IEnumerable,我尝试使用skipWhile 和 TakeWhile 但没有找到解决方案,输入如下如下:


Couple("a",false)

Couple("b",false)

Couple("c",true),

Couple("d",false)

Couple("e",false)

Couple("f",true),

Couple("g",true),

Couple("h",true),

Couple("i",false)

Couple("j",true),

Couple("k",true),

Couple("l",false)

Couple("m",false)

结果应该是 7 个 IEnumerables


list1: Couple("a",false)

       Couple("b",false)

list2: Couple("c",true)

list3: Couple("d",false)

       Couple("e",false)

list4: Couple("f",true)

       Couple("g",true)

       Couple("h",true)

list5: Couple("i",false)

list6: Couple("j",true)

       Couple("k",true)

list7: Couple("l",false) 

       Couple("m",false)

有什么帮助吗?


慕莱坞森
浏览 109回答 3
3回答

开满天机

尝试这个:public static IEnumerable<IList<Couple>> Split(IEnumerable<Couple> couples){&nbsp; &nbsp; using (var enumerator = couples.GetEnumerator())&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (!enumerator.MoveNext())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var current = enumerator.Current;&nbsp; &nbsp; &nbsp; &nbsp; var group = new List<Couple> { current };&nbsp; &nbsp; &nbsp; &nbsp; while (enumerator.MoveNext())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var next = enumerator.Current;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (current.Indicator.Equals(next.Indicator))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; group.Add(next);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return group;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; group = new List<Couple> { next };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current = next;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; yield return group;&nbsp; &nbsp; }}例子:var couples = new List<Couple>&nbsp;{&nbsp; &nbsp; new Couple("a",false),&nbsp; &nbsp; new Couple("b",false),&nbsp; &nbsp; new Couple("c",true),&nbsp; &nbsp; new Couple("d",false),&nbsp; &nbsp; new Couple("e",false),&nbsp; &nbsp; new Couple("f",true),&nbsp; &nbsp; new Couple("g",true),&nbsp; &nbsp; new Couple("h",true),&nbsp; &nbsp; new Couple("i",false),&nbsp; &nbsp; new Couple("j",true),&nbsp; &nbsp; new Couple("k",true),&nbsp; &nbsp; new Couple("l",false),&nbsp; &nbsp; new Couple("m",false),};var groupNr = 1;foreach (var couplesGroup in Split(couples)){&nbsp; &nbsp; Console.WriteLine($"List {groupNr++}: ");&nbsp; &nbsp; foreach (var couple in couplesGroup)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"{couple.Text, 10}, {couple.Indicator}");&nbsp; &nbsp; }&nbsp; &nbsp; Console.WriteLine();}

浮云间

这是分割序列的通用扩展方法。它需要一个函数来检查两个连续的元素,并确定这些元素是否应该拆分。true意味着分裂元素的结果。均值的结果false不会拆分元素,而是将它们放在同一子序列中。public static class EnumerableExtensions{&nbsp; &nbsp; /// <summary>Splits a sequence to subsequences according to a specified&nbsp; &nbsp; /// predicate.</summary>&nbsp; &nbsp; /// <param name="splitPredicate">A function to determine if two consecutive&nbsp; &nbsp; /// elements should be split.</param>&nbsp; &nbsp; public static IEnumerable<TSource[]> SplitByPredicate<TSource>(&nbsp; &nbsp; &nbsp; &nbsp; this IEnumerable<TSource> source,&nbsp; &nbsp; &nbsp; &nbsp; Func<TSource, TSource, bool> splitPredicate)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var enumerator = source.GetEnumerator();&nbsp; &nbsp; &nbsp; &nbsp; bool finished = false;&nbsp; &nbsp; &nbsp; &nbsp; TSource previous = default;&nbsp; &nbsp; &nbsp; &nbsp; using (enumerator)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!enumerator.MoveNext()) yield break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (!finished)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return GetSubsequence().ToArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; IEnumerable<TSource> GetSubsequence()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return enumerator.Current;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; previous = enumerator.Current;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!enumerator.MoveNext()) { finished = true; break; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (splitPredicate(previous, enumerator.Current)) break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}使用示例:var subsequences = couples.SplitByPredicate(&nbsp; &nbsp; (x, y) => x.Indicator != y.Indicator);

慕尼黑的夜晚无繁华

这是一个不使用 Linq 的解决方案:class Program{&nbsp; public class Couple&nbsp; {&nbsp; &nbsp; public string Text;&nbsp; &nbsp; public bool Indicator;&nbsp; &nbsp; public Couple(string text, bool indicator)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; Text = text;&nbsp; &nbsp; &nbsp; Indicator = indicator;&nbsp; &nbsp; }&nbsp; }&nbsp; static void Main(string[] args)&nbsp; {&nbsp; &nbsp; var list = new List<Couple>();&nbsp; &nbsp; list.Add(new Couple("a", false));&nbsp; &nbsp; list.Add(new Couple("b", false));&nbsp; &nbsp; list.Add(new Couple("c", true));&nbsp; &nbsp; list.Add(new Couple("d", false));&nbsp; &nbsp; list.Add(new Couple("e", false));&nbsp; &nbsp; list.Add(new Couple("f", true));&nbsp; &nbsp; list.Add(new Couple("g", true));&nbsp; &nbsp; list.Add(new Couple("h", true));&nbsp; &nbsp; list.Add(new Couple("i", false));&nbsp; &nbsp; list.Add(new Couple("j", true));&nbsp; &nbsp; list.Add(new Couple("k", true));&nbsp; &nbsp; list.Add(new Couple("l", false));&nbsp; &nbsp; list.Add(new Couple("m", false));&nbsp; &nbsp; var result = new List<List<Couple>>();&nbsp; &nbsp; int index = 0;&nbsp; &nbsp; bool last = list[0].Indicator;&nbsp; &nbsp; result.Add(new List<Couple>());&nbsp; &nbsp; foreach ( var item in list )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; if ( item.Indicator != last )&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; index++;&nbsp; &nbsp; &nbsp; &nbsp; result.Add(new List<Couple>());&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; last = item.Indicator;&nbsp; &nbsp; &nbsp; result[index].Add(item);&nbsp; &nbsp; }&nbsp; &nbsp; for ( index = 0; index < result.Count; index++ )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; Console.WriteLine($"List n°{index}");&nbsp; &nbsp; &nbsp; foreach ( var item in result[index] )&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"&nbsp; text: {item.Text}");&nbsp; &nbsp; }&nbsp; &nbsp; Console.WriteLine("");&nbsp; &nbsp; Console.ReadKey();&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP