实体框架 - Linq to Entity - 如何通过动态排序查询? [英] Entity Framework - Linq to Entity - How to query with sort by dynamic?
本文介绍了实体框架 - Linq to Entity - 如何通过动态排序查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用MS SQL server 2005,VS2008,Ms Entity框架。
我的问题是:如何使用动态排序字段进行查询(使用Linq)?
我试过这个,但似乎不起作用:
IQueryable< Order> orders = Context.Order.OrderBy(sortByFieldName);
是否有解决此问题的方法?我只是不想使用storedprocedure。
提前多谢。
解决方案
你的sortByFieldName参数是否是包含它的字符串。在它开始?
OrderBy查询构建器方法需要获取它的参数如下:
Context.Order.OrderBy(" it.FieldName");
在这里查看更多详细信息:
http://msdn.microsoft.com/en-us/library/bb358828.aspx
I'm doing with MS SQL server 2005, VS2008, Ms Entity framework.
My problem is: how can I make a query (using Linq) with dynamic sort field?
I've tried this but it seems not to work:
IQueryable<Order> orders = Context.Order.OrderBy(sortByFieldName);
Is there any Solution for this problem? I just dont want to use storedprocedure.
Thanks alot in advance.
解决方案
Hi,
Did your sortByFieldName parameter is a string that include the it. at it's begining?
An OrderBy query builder method needs to get it's parameter as follows:
Context.Order.OrderBy("it.FieldName");
See more details here:
http://msdn.microsoft.com/en-us/library/bb358828.aspx
这篇关于实体框架 - Linq to Entity - 如何通过动态排序查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文