LINQ casting with a Data.DataTableCollection

走远了吗. 提交于 2019-12-11 06:36:24

问题


I have the following VB.NET code that I am using to sort a Data.DataTable by column count.

For Each dtTarget As Data.DataTable In _
    From x In Target.Tables _
    Where DirectCast(x, Data.DataTable).Rows.Count > 0 _
    Order By DirectCast(x, Data.DataTable).Columns.Count
...
Next

Is there a way to indicate that x is a Data.DataTable without having to DirectCast it each time it is referenced (twice in this case) in the LINQ query?


回答1:


Something like:

Target.Tables.Cast<Data.DataTable>()

and then make your query of that. And you should properly refactor you code into several lines to make it more readable.

(this is C# - but I will translate it, 2 sec)

Translated:

Dim query = From x In Target.Tables.Cast(Of Data.DataTable)() _
 Where x.Rows.Count > 0 _
 Order By x.Columns.Count _
 Select x

For Each dtTarget As var In query
   ...
Next


来源:https://stackoverflow.com/questions/3515904/linq-casting-with-a-data-datatablecollection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!