我将多个数组聚合成一个“大数组”时遇到困难,我认为这应该是可能的,但我不能让我的头
考虑一些返回一些dummyObject数组的方法
public class DummyObjectReceiver { public DummyObject[] GetDummyObjects { -snip- } }
现在我有这个地方:
public class Temp { public List<DummyObjectReceiver> { get; set; } public DummyObject[] GetAllDummyObjects () { //here's where I'm struggling (in linq) - no problem doing it using foreach'es... ;) } }
希望有一点清楚我想要实现的目标
(作为额外的我想要排序这个数组的一个int值DummyObject有… – 但是orderby应该没有问题,我希望;)
您可以使用SelectMany方法将数组中的数组返回到数组中.
原文链接:https://www.f2er.com/javaschema/281370.htmlpublic class DummyObject { public string Name; public int Value; } public class DummyObjectReceiver { public DummyObject[] GetDummyObjects() { return new DummyObject[] { new DummyObject() { Name = "a",Value = 1 },new DummyObject() { Name = "b",Value = 2 } }; } } public class Temp { public List<DummyObjectReceiver> Receivers { get; set; } public DummyObject[] GetAllDummyObjects() { return Receivers.SelectMany(r => r.GetDummyObjects()).OrderBy(d => d.Value).ToArray(); } }
例:
Temp temp = new Temp(); temp.Receivers = new List<DummyObjectReceiver>(); temp.Receivers.Add(new DummyObjectReceiver()); temp.Receivers.Add(new DummyObjectReceiver()); temp.Receivers.Add(new DummyObjectReceiver()); DummyObject[] result = temp.GetAllDummyObjects();