LINQ query on a DataTable

前端 未结 23 1786
执笔经年
执笔经年 2020-11-22 01:59

I\'m trying to perform a LINQ query on a DataTable object and bizarrely I am finding that performing such queries on DataTables is not straightforward. For example:

23条回答
  •  面向向阳花
    2020-11-22 02:30

    You can get it work elegant via linq like this:

    from prod in TenMostExpensiveProducts().Tables[0].AsEnumerable()
    where prod.Field("UnitPrice") > 62.500M
    select prod
    

    Or like dynamic linq this (AsDynamic is called directly on DataSet):

    TenMostExpensiveProducts().AsDynamic().Where (x => x.UnitPrice > 62.500M)
    

    I prefer the last approach while is is the most flexible. P.S.: Don't forget to connect System.Data.DataSetExtensions.dll reference

提交回复
热议问题