簡體   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