我正在对列表中的项目进行初始化和排列,如下所示:
MyArray[] Arrayitems = SomeOtherList
.Select(x => new MyArray[]
{
ArrayPar1 = x.ListPar1,
}).ToArray()
我有一个辅助列表,我想将其添加到初始化程序中的同一个内联数组,如下所示 ():
MyArray[] Arrayitems = SomeOtherList
.Select(x => new MyArray[]
{
ArrayPar1 = x.ListPar1,
}).ToArray()
.Join(
MyArray[] Arrayitems = SomeOtherListNo2
.Select(x => new MyArray[]
{
ArrayPar1 = x.ListPar1,
}).ToArray()
);
这是可能的还是我必须在初始选择语句之前组合所有内容?
最佳答案
您可以使用 Concat
:
MyArray[] Arrayitems = SomeOtherList.Concat(SomeOtherListNo2)
.Select(x => new MyArray()
{
ArrayPar1 = x.ListPar1,
}).ToArray();
如果项目可以包含在两个列表中,并且您只希望它们在结果中出现一次,则可以使用 Union
:
MyArray[] Arrayitems = SomeOtherList.Union(SomeOtherListNo2)
.Select(x => new MyArray()
{
ArrayPar1 = x.ListPar1,
}).ToArray();
关于c# - 内联组合数组 - LINQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42222337/