我正在从列表中的项目初始化和数组,如下所示:
MyArray[] Arrayitems = SomeOtherList
.Select(x => new MyArray[]
{
ArrayPar1 = x.ListPar1,
}).ToArray()
我有一个辅助列表,我想在初始化器中添加到内联的相同数组中,类似于this():
MyArray[] Arrayitems = SomeOtherList
.Select(x => new MyArray[]
{
ArrayPar1 = x.ListPar1,
}).ToArray()
.Join(
MyArray[] Arrayitems = SomeOtherListNo2
.Select(x => new MyArray[]
{
ArrayPar1 = x.ListPar1,
}).ToArray()
);
这是可能的还是我必须在初始选择语句之前组合所有内容?
答案 0 :(得分:6)
您可以使用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();