[英]Best way to add an array of objects to a list of extended objects
I have an Array Customers[]
named 'customerArray' and I have a Generic List List<ExtendedCustomers>
named 'extendedCustomerList' 我有一个名为“ customerArray”的数组
Customers[]
,我有一个名为“ extendedCustomerList”的通用列表List<ExtendedCustomers>
The ExtendedCustomer
class contains some properties, and one of them is 'Customer' (Yes, thats the same as the objects in the array), like this: ExtendedCustomer
类包含一些属性,其中之一是“ Customer”(是,与数组中的对象相同),如下所示:
public class ExtendedCustomer {
public Customer { get; set; }
public OtherProperty { get; set; }
....
}
What is the fastest / best / easiest / best performing / most beautiful way to add the array with Customers to the list with ExtendedCustomers? 将带有客户的阵列添加到带有ExtendedCustomers的列表中的最快/最佳/最简单/最佳性能/最美观的方法是什么? The other properties in ExtendedCustomer can keep ther default NULL value.
ExtendedCustomer中的其他属性可以保留默认的NULL值。
I don't like loops 我不喜欢循环
You can use AddRange() with a projection from customers to extended customers: 您可以将AddRange()与客户投影到扩展客户一起使用:
extendedCustomerList.AddRange(customerArray.Select(
c => new ExtendedCustomer() {
Customer = c
}));
Customer[] customers = ...;
List<ExtendedCustomer> extendedCustomers = ...;
extendedCustomers.AddRange(
customers.Select(c => new ExtendedCustomer{ Customer = c }));
Using LINQ: 使用LINQ:
extendedCustomerList.AddRange(
from customer in customerArray
select new ExtendedCustomer {
Customer = customer
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.