Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在实体框架和ObjectContext中使用OrderBy时出现问题。CreateQuery(T)_C#_Asp.net Mvc_Entity Framework_Repository_Sql Order By - Fatal编程技术网

C# 在实体框架和ObjectContext中使用OrderBy时出现问题。CreateQuery(T)

C# 在实体框架和ObjectContext中使用OrderBy时出现问题。CreateQuery(T),c#,asp.net-mvc,entity-framework,repository,sql-order-by,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)

我在使用此方法时遇到问题:

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;
}
public ObjectQuery DoQuery(字符串列名、int-maximumRows、int-startRowIndex)
{
var result=(ObjectQuery)\u ctx.CreateQuery
(“[”+typeof(E).Name+“]”).OrderBy(/*???*/).Skip(startRowIndex).Take(maximumrrows);
返回结果;
}
我不确定在
OrderBy
部分需要做什么。我尝试忽略了
.OrderBy
,但它最终给了我一个错误,告诉我需要对其进行排序,以便我可以获取一定数量的行

我想按ID字段订购。在一种情况下,我将实体名称作为行,该实体的ID命名为
RowID

有人知道我怎么能按ID字段订购吗


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

当我创建存储库时,我假设每个EntityObject都有ID属性。然后我创建了存储库:

public class Repository<T> : IRepository<T> where T : EntityObject, IBasicEntityInfo
然后

OrderBy(a => a.ID)
自动生成的EntityObject类没有实现IBasiEntityInfo接口,但我使用T4生成:

public partial class User : IBasicEntityInfo ,ILastModificationInfo
public partial class Project : IBasicEntityInfo ,ILastModificationInfo
public partial class Group : IBasicEntityInfo ,ILastModificationInfo
public partial class GroupUser : IBasicEntityInfo 
public partial class OperationSystem : IBasicEntityInfo ,ILastModificationInfo
T4很简单:

<#@ template language="C#" #>
<#@ output extension="cs" #>
<#@ import namespace="System.Collections.Generic" #>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Web;

<# 
    String[] classNames = new String[] {"User","Project","Group","GroupUser","OperationSystem","TaskType","Priority","Severity","Status","Version","Platform","Task","TaskUser","Attachment","Comment","Setting"}; 
#>
namespace CamelTrap.Models
{
<# foreach(String className in classNames) { #>
    public partial class <#= className #> : IBasicEntityInfo
    {   
    }
<# } #>
}

使用制度;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用System.Web;
名称空间:trap.Models
{
公共部分类:IBasicEntityInfo
{   
}
}
做出这样的假设也解决了其他问题,使我免于创建复杂的表达式。

您只需:

.OrderBy("it.Id")

…假定您的ID字段称为“ID”。在查询生成器中,“it”表示实体。请参阅以供参考。

这在许多方面都很有用(我们做的几乎是相同的事情),但实际上无助于解决他遇到的问题。它确实解决了问题,因为您可以使用OrderBy(a=>a.ID)。我的存储库通过实现IBasicEntityInfo知道每个对象都有ID属性。在他所处的级别上,他不知道
E
的具体类型。因此,您的评论中的OrderBy将不起作用。他也不能可靠地将
E
转换为
IBasicEntityInfo
,除非他对
E
有限制,这有其自身的缺点。
.OrderBy("it.Id")