[英]Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'
I have the following code: 我有以下代码:
List<String> rows =
allDetails.Tables[3].AsEnumerable().OrderBy(dr => dr.Field<string> ("JobTitle")).ToList();
The error is: 错误是:
Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'
DataTable.AsEnumerable()
returns an IEnumerable<DataRow>
which you can use for LINQ queries like yours. DataTable.AsEnumerable()
返回一个IEnumerable<DataRow>
,您可以将其用于像您这样的LINQ查询。 But you cannot use ToList
on it and expect that it is a List<String>
. 但是您不能在其上使用
ToList
并期望它是List<String>
。 Therefore you have to select the string-field: 因此,您必须选择字符串字段:
List<String> orderedJobTitles = allDetails.Tables[3].AsEnumerable()
.OrderBy(dr => dr.Field<string>("JobTitle"))
.Select(dr => dr.Field<string>("JobTitle"))
.ToList();
You could also select the field before you order by it if you don't need the DataRow
anyway: 如果仍然不需要
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.