I have an Array Customers[]
named 'customerArray' and I have a Generic List List<ExtendedCustomers>
named 'extendedCustomerList'
The ExtendedCustomer
class contains some properties, and one of them is 'Customer' (Yes, thats the same as the objects in the array), like this:
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? The other properties in ExtendedCustomer can keep ther default NULL value.
I don't like loops
You can use AddRange() with a projection from customers to extended customers:
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:
extendedCustomerList.AddRange(
from customer in customerArray
select new ExtendedCustomer {
Customer = customer
}
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.