在没有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));更多推荐
发布评论