合并使用LINQ 2阵列 [英] Merge 2 arrays using LINQ
本文介绍了合并使用LINQ 2阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个简单的数组,我想使用连接LINQ合并:
INT [] NUM1 =新INT [ ] {1,55,89,43,67,-3};
INT [] = NUM2新INT [] {11,35,79,23,7,-10};
VAR的结果=从NUM2
选择结果NUM1
N1 N2距离;
解决方案
您可以将它使用的 的毗连
和的 的ToArray
,像这样的:
VAR解析度= num1.Concat(NUM2).ToArray();
这将会把 NUM2
中的所有元素后,元素 NUM1
,生产 RES
,看起来像
INT [] {1,55,89,43,67,-3,11,35,79,23,7,-10};
编辑:(在响应评论:我怎么能还排序要么allNumbers和RES?)
一旦你的两个数组合并,则可以使用排序依据
来排序因此,像这样的:
VAR解析度= num1.Concat(NUM2).OrderBy(v => 5).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屋!
查看全文