简体   繁体   English

无法隐式转换类型&#39;System.Collections.Generic.List <System.Data.DataRow> &#39;到&#39;System.Collections.Generic.List <string> &#39;

[英]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.

相关问题 无法将类型&#39;System.Collections.Generic.List&#39;隐式转换为&#39;string&#39; - Cannot implicitly convert type 'System.Collections.Generic.List' to 'string' 无法隐式转换类型&#39;System.Collections.Generic.list <fooClass> &#39;到&#39;System.Collections.Generic.List <T> - Cannot Implicitly convert type 'System.Collections.Generic.list<fooClass>' to 'System.Collections.Generic.List<T> 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;到&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List 无法隐式转换类型&#39;System.Collections.Generic.List <MyProduct> &#39;到&#39;System.Collections.Generic.List <T> - Cannot implicitly convert type 'System.Collections.Generic.List<MyProduct>' to 'System.Collections.Generic.List<T> 如何修复“无法将类型System.Collections.Generic.List &lt;&gt;隐式转换为System.Collections.Generic.List &lt;&gt;” - How to fix 'Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>' 无法隐式转换类型'System.Collections.Generic.List<x> ' 到 'System.Collections.Generic.List<y> '</y></x> - Cannot implicitly convert type 'System.Collections.Generic.List<x>' to 'System.Collections.Generic.List<y>' 无法隐式转换类型&#39;System.Collections.Generic.List &lt; <anonymous type> &gt;”到“ System.Collections.Generic.List <string> &#39; - Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type>>' to 'System.Collections.Generic.List<string>' C#:无法隐式转换类型“System.Collections.Generic.List”<char> &#39; 到 &#39;System.Collections.Generic.List<string> &#39; - C#: Cannot implicitly convert type 'System.Collections.Generic.List<char>' to 'System.Collections.Generic.List<string>' 无法隐式转换类型&#39;System.Collections.Generic.List <Data.emp> &#39;到&#39;System.Collections.Generic.List <User.Employee> &#39; - Cannot implicitly convert type 'System.Collections.Generic.List<Data.emp>' to 'System.Collections.Generic.List<User.Employee>' 无法将类型&#39;System.Collections.Generic.List &lt;&gt;&#39;隐式转换为方法 - Cannot implicitly convert type 'System.Collections.Generic.List<>' to method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM