使用 LINQ 获取对集 [英] Getting pair-set using LINQ

查看:16
本文介绍了使用 LINQ 获取对集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有一个列表

IList<int> list = new List<int>();
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} }

推荐答案

这将为您提供一个匿名pair"对象数组,其中 A 和 B 属性对应于 pair 元素.

This will give you an array of anonymous "pair" objects with A and B properties corresponding to the pair elements.

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

这篇关于使用 LINQ 获取对集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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