C# 将对象数组添加到扩展对象列表的最佳方法
我有一个名为“customerArray”的数组C# 将对象数组添加到扩展对象列表的最佳方法,c#,C#,我有一个名为“customerArray”的数组Customers[]和一个名为“extendedCustomerList”的通用列表List ExtendedCustomer类包含一些属性,其中一个是“Customer”(是的,与数组中的对象相同),如下所示: public class ExtendedCustomer { public Customer { get; set; } public OtherProperty { get; set; } .... }
Customers[]
和一个名为“extendedCustomerList”的通用列表List
ExtendedCustomer
类包含一些属性,其中一个是“Customer”(是的,与数组中的对象相同),如下所示:
public class ExtendedCustomer {
public Customer { get; set; }
public OtherProperty { get; set; }
....
}
将客户阵列添加到ExtendedCustomers列表中的最快/最佳/最简单/性能最佳/最漂亮的方法是什么?ExtendedCustomer中的其他属性可以保留默认的空值
我不喜欢循环可以用于从客户到扩展客户的投影:
extendedCustomerList.AddRange(customerArray.Select(
c => new ExtendedCustomer() {
Customer = c
}));
您可以使用从客户到扩展客户的投影:
extendedCustomerList.AddRange(customerArray.Select(
c => new ExtendedCustomer() {
Customer = c
}));
Customer[]客户=。。。;
列出扩展客户=。。。;
extendedCustomers.AddRange(
选择(c=>newextendedcustomer{Customer=c});
客户[]客户=。。。;
列出扩展客户=。。。;
extendedCustomers.AddRange(
选择(c=>newextendedcustomer{Customer=c});
使用LINQ:
extendedCustomerList.AddRange(
from customer in customerArray
select new ExtendedCustomer {
Customer = customer
}
);
使用LINQ:
extendedCustomerList.AddRange(
from customer in customerArray
select new ExtendedCustomer {
Customer = customer
}
);