开发者

Dynamic Linq query on Datatable

开发者 https://www.devze.com 2023-01-09 18:04 出处:网络
I want to make the following query dynamic. var t = from r in dt.AsEnumerable() orderby r.Field< int >(\"id\") ascending

I want to make the following query dynamic.

var t = from r in dt.AsEnumerable()  
orderby r.Field< int >("id") ascending
select r;

void query(string sorttype,string sortorder)

Is it possbile to change the order by part so that those paramater will come from parameters of a function like so开发者_运维问答rttype for id in here and sortorder for ascending.


I think this ought to do it:

IEnumerable query<sorttype>(DataTable dt, string sortorder) {
    return dt.GetList().OrderBy(row => row.Field<sorttype>(sortorder));
}

Note that sorttype is a generic type parameter, not a string.

0

精彩评论

暂无评论...
验证码 换一张
取 消