使用LINQ合并2个数组 [英] Merge 2 arrays using LINQ
本文介绍了使用LINQ合并2个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int [] num1 = new int [ ] {1,55,89,43,67,-3};
int [] num2 = new int [] {11,35,79,23,7,-10};
var result = from n1 in num1
from n2 in num2
select result;
解决方案
var res = num1.Concat(num2).ToArray();
这将把 num2
的所有元素元素 num1
,生成 res
,看起来像
int [] {1,55,89,43,67,-3,11,35,79,23,7,-10};
编辑:(回覆一则评论:我怎么也排序allNumbers和res?)
一旦您的两个数组合并,您可以使用 OrderBy
来排序结果如下:
var res = num1.Concat(num2).OrderBy(v => v).ToArray );
I have two simple array and I would like to merge using join linq:
int[] num1 = new int[] { 1, 55, 89, 43, 67, -3 };
int[] num2 = new int[] { 11, 35, 79, 23, 7, -10 };
var result = from n1 in num1
from n2 in num2
select result;
解决方案
You can do it using Concat
and ToArray
, like this:
var res = num1.Concat(num2).ToArray();
This will put all elements of num2
after elements of num1
, producing res
that looks like
int[] { 1, 55, 89, 43, 67, -3, 11, 35, 79, 23, 7, -10 };
EDIT : (in response to a comment: "how can I also sort either allNumbers and res?")
Once your two arrays are merged, you can use OrderBy
to sort the result, like this:
var res = num1.Concat(num2).OrderBy(v=>v).ToArray();
这篇关于使用LINQ合并2个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文