使用LINQ进行配对设置

当我有一个清单

IList list = new List(); list.Add(100); list.Add(200); list.Add(300); list.Add(400); list.Add(500); 

提取对的方法是什么

 Example : List elements {100,200,300,400,500} Expected Pair : { {100,200} ,{200,300} ,{300,400} ,{400,500} } 

这将为您提供一组匿名“对”对象,其中A和B属性对应于对元素。

 var pairs = list.Where( (e,i) => i < list.Count - 1 ) .Select( (e,i) => new { A = e, B = list[i+1] } ); 

LINQ最优雅的方式: list.Zip(list.Skip(1), Tuple.Create)

一个真实的例子:这个扩展方法采用一组点( Vector2 )并产生一个’加入点’所需的行集合( PathSegment )。

 static IEnumerable JoinTheDots(this IEnumerable dots) { var segments = dots.Zip(dots.Skip(1), (a,b) => new PathSegment(a, b)); return segments; } 

你可以使用for循环:

 var pairs = new List(); for(int i = 0; i < list.Length - 1; i++) pairs.Add(new [] {list[i], list[i + 1]); 

你也可以使用LINQ,但它更丑陋:

 var pairs = list.Take(list.Count - 1).Select((n, i) => new [] { n, list[i + 1] }); 

编辑 :您甚至可以在原始的IEnumerable ,但它更加丑陋:

 var count = list.Count(); var pairs = list .SelectMany((n, i) => new [] { new { Index = i - 1, Value = n }, new { Index = i, Value = n } }) .Where(ivp => ivp.Index >= 0 && ivp.Index < count - 1) //We only want one copy of the first and last value .GroupBy(ivp => ivp.Index, (i, ivps) => ivps.Select(ivp => ivp.Value)); 

更一般的是:

  public static IEnumerable Pairwise(this IEnumerable values, int count, Func pairCreator) { if (count < 1) throw new ArgumentOutOfRangeException("count"); if (values == null) throw new ArgumentNullException("values"); if (pairCreator == null) throw new ArgumentNullException("pairCreator"); int c = 0; var data = new TSource[count]; foreach (var item in values) { if (c < count) data[c++] = item; if (c == count) { yield return pairCreator(data); c = 0; } } } 

以下解决方案使用zip方法。 Zip原始列表和originalList.Skip(1),以便获得所需的结果。

  var adjacents = originalList.Zip(originalList.Skip(1), (a,b) => new {N1 = a, N2 = b}); 

在我的头顶,完全未经测试:

 public static T Pairwise(this IEnumerable list) { T last; bool firstTime = true; foreach(var item in list) { if(!firstTime) return(Tuple.New(last, item)); else firstTime = false; last = item; } } 

使用.Windowed()

 var source = new[] {100,200,300,400,500}; var result = source.Windowed(2).Select(x => Tuple.Create(x.First(),x.Last()));