[英]C# - Convert a Type Array to a Generic List
代碼片段:
ShippingPeriod[] arrShippingPeriods;
.
.
.
List<ShippingPeriod> shippingPeriods = ShippingPeriodList.ToList<ShippingPeriod>();
最后一行不會編譯,我得到的錯誤是:
“'ShippingPeriod []'不包含'ToList'的定義,並且最好的擴展方法重載'System.Linq.Enumerable.ToList(System.Collections.Generic.IEnumerable)'有一些無效的參數”
嘗試這個:
ShippingPeriod [] arrShippingPeriods;
//init and populate array
IList<ShippingPeriods> lstShippingPeriods =
arrShippingPeriods.ToList<ShippingPeriods>();
您需要在數組對象上調用ToList而不是數組中包含的對象類。
正如其他人所說,你需要在你的陣列上調用ToList
。 您尚未顯示ShippingPeriodList
是什么。
但是,當你理解這一點時,請注意你不需要提供類型參數,因為類型推斷會為你做。 換句話說,這應該工作正常:
List<ShippingPeriod> list = arrShippingPeriods.ToList();
另一種選擇是:
ShippingPeriod [] arrShippingPeriods;
var lstShippingPerios=new List<ShippingPeriod>(arrShippingPeriods);
由於數組已經實現了IEnumerable,因此可以將它傳遞給List構造函數。
希望這可以幫助
我得到了解決方案!
你必須把“使用System.Linq;” 在你的班級的標題中,就是這樣。 放置它時,數組識別toList()命令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.