按顺序而不是LINQ中的条件加入2个列表 [英] Join 2 lists by order instead of condition in LINQ

查看:111
本文介绍了按顺序而不是LINQ中的条件加入2个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何联接两个等长列表(以生成等长的第三个列表),在这里我不想指定条件,而只是依靠两个列表中的项目顺序.

How can I join 2 lists of equal lengths (to produce a 3rd list of equal length) where I do not want to specify a condition but simply rely on the order of items in the 2 lists.

例如,我如何加入:

{1,2,3,4} with {5,6,7,8}

产生:

{{1,5}, {2,6}, {3,7}, {4,8}}

我尝试了以下操作:

from i in new []{1,2,3,4}
from j in new []{5,6,7,8}
select new { i, j }

但是会产生交叉连接.使用连接时,我总是需要指定"on".

but this produces a cross join. When I use join, I always need to specify the "on".

推荐答案

您可以使用在第一个列表中选择,使用项目索引并访问第二个列表中的元素:

You could use Select in the first list, use the item index and access the element on the second list:

var a = new [] {1,2,3,4};
var b = new [] {5,6,7,8};

var qry = a.Select((i, index) => new {i, j = b[index]}); 

这篇关于按顺序而不是LINQ中的条件加入2个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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