在没有For循环的情况下在Linq中配对集合(Pairing Collections in Linq without a For Loop)

编程入门 行业动态 更新时间:2024-10-21 19:50:01
在没有For循环的情况下在Linq中配对集合(Pairing Collections in Linq without a For Loop)

我有两个集合

int[] numbers = {5, 2, 1, 5}; string[] words = {"flibble", "bobble", "double", "dumble"};

我想在第二个集合上使用第一个linq表达式。 使用循环,我会做下面的事情

List<string> results = new List<string>(); for(int i = 0; i<numbers.Count(), i++) { results.Add(words[i].SubString(numbers[i]); } return results;

然而,这涉及到创建一个没有真正原因的列表......有没有办法在Linq中做到这一点?

I have two collections

int[] numbers = {5, 2, 1, 5}; string[] words = {"flibble", "bobble", "double", "dumble"};

I want to use a linq expression on the second collection using the first. Using a loop I'd do the following

List<string> results = new List<string>(); for(int i = 0; i<numbers.Count(), i++) { results.Add(words[i].SubString(numbers[i]); } return results;

However this involves creating a list for no real reason... is there a way to do this in Linq?

最满意答案

我猜你正在寻找Enumerable.Zip() :

numbers.Zip(words, (n, w) => w.Substring(n));

I'm guessing you're looking for Enumerable.Zip():

numbers.Zip(words, (n, w) => w.Substring(n));

更多推荐

本文发布于:2023-07-24 10:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:情况下   Linq   Pairing   Collections   Loop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!