Linq-to-Entities Dynamic sorting

前端 未结 5 647
无人共我
无人共我 2021-02-04 03:51

This is my query, how can I use string as orderby parameter?

string sortColumn=\"Title\";

var  items = (from ltem in ctxModel.Items
              where ltem.Ite         


        
5条回答
  •  孤独总比滥情好
    2021-02-04 04:14

    That query looks like you're using custom databinding and/or ObjectDataSource, regardless, there is a way to do this using an extension method which takes a sort expression and dynamically appends an OrderBy() call (expression) to the linq query. I documented how in blog post a while back, which coincidentally was part of this SO question. If you need more than that, you could use dynamic linq which is documented pretty well by scottgu.

    EDIT: using the extension method would make it look like

    string sortColumn="Title";
    
        var  items = (from ltem in ctxModel.Items
                      where ltem.ItemID == vId
                      select ltem).Skip(PageSize * PageIndex).Take(PageSize).OrderBy(sortColumn);
    

提交回复
热议问题