views:

613

answers:

2

Hi,

I'm having troubles using this method:

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;
}

I'm not sure what needs to go in the OrderBy section. I've tried leaving out .OrderBy but it ends up giving me an error saying that it needs to be ordered so that I can take a certain amount of rows.

I want to order by the ID field. In one case I have the entity name as Row, the ID of this entity is named RowID.

Does anyone know how I could order by the ID field?

Note: This is a generic repository using the entity framework. The columnName passed in is the column I would like to order by, but it will probably only be the name of the ID field.

+1  A: 

When I created my repository, I assumed that every EntityObject has ID property. Then I Created repository:

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

IBasicEntityInfo:

public interface IBasicEntityInfo
{
    int ID { get; set; }
}

and then

OrderBy(a => a.ID)

Automatically generated EntityObject classes don't implement IBasicEntityInfo interface, but I use T4 to generate:

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 is simple:

<#@ 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
 {   
 }
<# } #>
}

Making such assumptions solved other problems too and saved me from creating complicated expressions.

LukLed
That's useful in many ways (we do almost the same thing), but doesn't actually help solve the problem he's having.
Craig Stuntz
It does solve the problem, because you can use OrderBy(a => a.ID). My repository knows that every object has ID property by implementing IBasicEntityInfo.
LukLed
Your solution is, of course, slightly easier:)
LukLed
At the level he's at, he doesn't know the specific type of `E`. So the OrderBy in your comment won't work. Nor can he reliably cast `E` to `IBasicEntityInfo`, unless he has a constraint on `E`, which has its own downsides.
Craig Stuntz
+2  A: 

You just do:

.OrderBy("it.Id")

...presuming your ID field is called "Id". In query builder, the "it" means the entity. See this page for reference.

Craig Stuntz