如何使用 NewExpression 中的参数作为 OrderBy 方法中使用的表达式?
首先,我使用 C# 4.0 和 EF 4.0 以及 POCO 对象来访问数据库。接下来,我创建一些网格(如 jqGrid),用于通过 ASP.NET MVC 2.0 显示数据库中的数据。该网格可以通过单击列标题来对数据进行排序。源代码可能如下所示。
// This method will generate data for jqGrid request.
// jqGridRequest contain several options about how to query data like
// Take 10 result
// Skip 50 rows
// Filter by something
// Order by column name
public JsonResult GetPeopleData(jqGridRequest req)
{
// This extension method has 2 parameters that are jqGridRequest and
// Expression<Func<T, object>> for creating object to be serialized.
// In this case, T is People type.
return DataContext.People.AsJqGridResult
(
req,
x => new
{
x.ID,
Name = x.FirstName + " " + x.LastName,
x.Age
}
)
}
一切正常。问题是,当我尝试在此网格中订购“名称”列时,jqGrid 将发送请求,告诉控制器按“名称”列对数据进行排序。但是,“名称”列在数据库中不存在,因为它只是某些列的组合值。
解决这个问题最简单的方法是创建一些代码来执行类似以下代码的操作。
DataContext.People.OrderBy(x => x.FirstName + " " + x.LastName);
但是,我需要创建一些方法来处理任何像这样的简单排序条件。在我搜索并尝试了有关表达的所有可能性之后。我刚刚发现我可以使用 NewExpression 中包含的一些数据来排序此查询。但我不知道如何将 NewExpression 对象中的参数转换/创建为表达式以用作 OrderBy 方法参数。
谢谢
First, I use C# 4.0 and EF 4.0 with POCO object to access database. Next, I create some grid (like jqGrid) for displaying data from database via ASP.NET MVC 2.0. This grid can order data by clicking at the column header. Source code could look like this.
// This method will generate data for jqGrid request.
// jqGridRequest contain several options about how to query data like
// Take 10 result
// Skip 50 rows
// Filter by something
// Order by column name
public JsonResult GetPeopleData(jqGridRequest req)
{
// This extension method has 2 parameters that are jqGridRequest and
// Expression<Func<T, object>> for creating object to be serialized.
// In this case, T is People type.
return DataContext.People.AsJqGridResult
(
req,
x => new
{
x.ID,
Name = x.FirstName + " " + x.LastName,
x.Age
}
)
}
Everything works fine. The question is when I try to order "Name" column in this grid, jqGrid will send the request that tell controller to order data by "Name" column. However, "Name" column does not exist on database because it's just a combined value of some column.
The easiest to solve this question is creating some code for doing something like the following code.
DataContext.People.OrderBy(x => x.FirstName + " " + x.LastName);
However, I need to create some method to handle any simple orderby condition like this. After I search and try any possibilities about expression. I just found that I can use some data that is contained in NewExpression to order this query. But I do not know how to convert/create Argument in NewExpression object to Expression for using as OrderBy method parameter.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Name
不存在于数据库中,但它确实存在于您投影到的匿名类型中。我不会尝试猜测所有扩展方法的作用。但如果你这样做:
......然后:
......那么它应该“正常工作”。
Name
doesn't exist on the DB, but it does exist in the anonymous type you're projecting onto.I'm not going to try and guess about what all your extension methods do. But if you did:
...and then:
...then it should "just work".