繁体   English   中英

将对象数组添加到扩展对象列表的最佳方法

[英]Best way to add an array of objects to a list of extended objects

我有一个名为“ customerArray”的数组Customers[] ,我有一个名为“ extendedCustomerList”的通用列表List<ExtendedCustomers>

ExtendedCustomer类包含一些属性,其中之一是“ Customer”(是,与数组中的对象相同),如下所示:

public class ExtendedCustomer {
     public Customer { get; set; }
     public OtherProperty { get; set; }
     ....
}

将带有客户的阵列添加到带有ExtendedCustomers的列表中的最快/最佳/最简单/最佳性能/最美观的方法是什么? ExtendedCustomer中的其他属性可以保留默认的NULL值。

我不喜欢循环

您可以将AddRange()与客户投影到扩展客户一起使用:

extendedCustomerList.AddRange(customerArray.Select(
    c => new ExtendedCustomer() {
        Customer = c
    }));
Customer[] customers = ...;
List<ExtendedCustomer> extendedCustomers = ...;
extendedCustomers.AddRange(
       customers.Select(c => new ExtendedCustomer{ Customer = c }));

使用LINQ:

extendedCustomerList.AddRange(
    from customer in customerArray
    select new ExtendedCustomer {
        Customer = customer
    }
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM