如何将商品通过索引? C#LINQ [英] How to group items by index? C# LINQ
本文介绍了如何将商品通过索引? C#LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有
变种输入=新INT [] {0,1,2,3,4,5};
我如何让他们分组为?
无功输出=新INT [] [] {新INT [] {0,1},新的INT [] {2,3},新的INT [] {4,5 }};
pferably使用LINQ $ P $
解决方案
输入
。选择((价值指数)=>新建{PairNum =指数/ 2,值})
.GroupBy(双=> pair.PairNum)
。选择(GRP => grp.Select(G => g.value).ToArray())
.ToArray()
Suppose I have
var input = new int[] { 0, 1, 2, 3, 4, 5 };
How do I get them grouped into pairs?
var output = new int[][] { new int[] { 0, 1 }, new int[] { 2, 3 }, new int[] { 4, 5 } };
Preferably using LINQ
解决方案
input
.Select((value, index) => new { PairNum = index / 2, value })
.GroupBy(pair => pair.PairNum)
.Select(grp => grp.Select(g => g.value).ToArray())
.ToArray()
这篇关于如何将商品通过索引? C#LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文