[英]Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'
[英]Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'
我有以下代碼:
List<String> rows =
allDetails.Tables[3].AsEnumerable().OrderBy(dr => dr.Field<string> ("JobTitle")).ToList();
錯誤是:
Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'
DataTable.AsEnumerable()
返回一個IEnumerable<DataRow>
,您可以將其用於像您這樣的LINQ查詢。 但是您不能在其上使用ToList
並期望它是List<String>
。 因此,您必須選擇字符串字段:
List<String> orderedJobTitles = allDetails.Tables[3].AsEnumerable()
.OrderBy(dr => dr.Field<string>("JobTitle"))
.Select(dr => dr.Field<string>("JobTitle"))
.ToList();
如果仍然不需要DataRow
,也可以在訂購之前選擇字段:
List<String> orderedJobTitles = allDetails.Tables[3].AsEnumerable()
.Select(dr => dr.Field<string>("JobTitle"))
.OrderBy(jobTitle => jobTitle)
.ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.