Convert Dataset to IQueryable or IEnumerable

2019-03-15 13:17发布

Since there is no Linq to DB2 yet (c'mon IBM!), and I want to deal with IQueryables or IEnumerables in my code, how would I convert a DataTable to an IQueryable? Or an IEnumerable?

I have an interface and a class that matches the columns in the datatable...

IQueryable<IMyData> GetAS400Data(..parameters..)
{
    DataSet d = GetData();
    ...
    //Some code to convert d to IQueryable<IMyData>
}

DataTable.Rows does not support .AsQueryable, since MSFT yanked it, so I'm not sure what to do here.

3条回答
仙女界的扛把子
2楼-- · 2019-03-15 13:26
table.AsEnumerable()...

table.AsEnumerable().AsQueryable()...

However, you'd need to write your own translation (Select) to your type; and the IQueryable<T> would still be using LINQ-to-Objects; the only purpose (in this scenario) of using IQueryable<T> over IEnumerable<T> would be to use expressions for some other reason - perhaps for the dynamic LINQ library.

查看更多
Anthone
3楼-- · 2019-03-15 13:26

Take a look here it seems that a provider with entity framework for DB2 exists.

查看更多
神经病院院长
4楼-- · 2019-03-15 13:45

you can use something like this.

DataSet ds = GetData();
DataTable dt= ds.Tables[0];
var query =
    from row in dt.AsEnumerable() 
    select new IMyData()
    {
        property1= row[0],
        property2= row[1]
    };
查看更多
登录 后发表回答