如何使用 NewExpression 中的参数作为 OrderBy 方法中使用的表达式?

发布于 2024-08-23 00:53:19 字数 1231 浏览 1 评论 0原文

首先,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请爱~陌生人 2024-08-30 00:53:19

Name 不存在于数据库中,但它确实存在于您投影到的匿名类型中。

我不会尝试猜测所有扩展方法的作用。但如果你这样做:

var q = from p in DataContext.People
        select new 
        { 
            Name = p.FirstName + " " + p.LastName
        };

......然后:

var r = q.OrderBy(p => p.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:

var q = from p in DataContext.People
        select new 
        { 
            Name = p.FirstName + " " + p.LastName
        };

...and then:

var r = q.OrderBy(p => p.Name);

...then it should "just work".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文