c# - 将 OrderBy 与 Entity Framework 和 ObjectContext.CreateQuery(T) 结合使用时出现问题

标签 c# asp.net-mvc entity-framework repository sql-order-by

我在使用这个方法时遇到了问题:

public ObjectQuery<E> DoQuery(string columnName, int maximumRows, int startRowIndex)
{
    var result = (ObjectQuery<E>)_ctx.CreateQuery<E>
    ("[" + typeof(E).Name + "]").OrderBy(/*???*/).Skip<E>(startRowIndex).Take(maximumRows);

    return result;
}

我不确定 OrderBy 部分需要做什么。我试过遗漏 .OrderBy 但它最终给我一个错误,说它需要排序,这样我才能获取一定数量的行。

我想按 ID 字段排序。在一种情况下,我将实体名称命名为 Row,该实体的 ID 名为 RowID

有谁知道我如何通过 ID 字段订购?

注意:这是一个使用 Entity Framework 的通用存储库。传入的 columnName 是我想要排序的列,但它可能只是 ID 字段的名称。

最佳答案

你只需要做:

.OrderBy("it.Id")

...假设您的 ID 字段称为“Id”。在查询生成器中,“它”表示实体。参见 this page供引用。

关于c# - 将 OrderBy 与 Entity Framework 和 ObjectContext.CreateQuery(T) 结合使用时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1694293/

相关文章:

entity-framework - 更改表和列名称映射 Entity Framework v4.3

c# - 将 XAML WPF 窗口转换为 WinForm

c# - 在C#中将项目添加到字符串数组列表

c# - ASP.NET 隐藏 <asp :Panel>/<th>/<tr>/<td> inside a ListView

c# - File.Exists——不想创建新文件

c# - 在 IEquatable 上重写等于

c# - 如何将新表中的新外键添加到现有记录

c# - Lambda 表达式 - 如何从 IEnumerable<Object> 向 where 子句提供值?

c# - 500 内部服务器错误 JsonResult mvc asp.net

c# - 如何在 ASP.NET Identity 中编辑用户