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

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

问题描述

当我有一个列表



 的IList< INT>名单=新名单,LT; INT>(); 
list.Add(100);
list.Add(200);
list.Add(300);
list.Add(400);
list.Add(500);



什么是提取对



的方式

 示例:列出元素{100,200,300,400,500} 

预计对:{{100,200},{200,300},{300,400},{400,500}}


解决方案

这会给你匿名的数组配对有了对象和,对应于该对元素中,b性质

  VAR对= list.Where((E,I)=> I< list.Count  -  1)
。选择((E,I)=>新建{A = E,b =列表第[i + 1]});


When i have a list

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

What is the way to extract a pairs

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

Expected Pair : { {100,200} ,{200,300} ,{300,400} ,{400,500} }

解决方案

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天全站免登陆