不能
执行以下操作:
var a = myDB.Where(p => p.field == "filter").OrderBy("it." + fieldname);
如何将“productID,productName”之类的字符串转换为OrderBy表达式
? 如果我的想法是正确的,那么问题可能是“如何将规范模式转换为表达式委托?”
问题是我不知道他们想要什么表,所以我不知道主键。我用泛型来表示表的类型。
public interface IRepository<E, C> where C : ObjectContext // E is an Entity
{
void Add(E entity);
void Del(E entity);
IList<E> Get(Expression<Func<E, bool>> filterLambda = null, //Where clause
Expression<Func<E, object>> orderbyLambda = null, //OrderBy
int? page = null, //Page to get
int? pageSize = null, //Page Size
Expression<Func<E, object>> selectLambda = null); //Populate Fields
int Count(Expression<Func<E, bool>> filterLambda = null);
bool SaveChanges();
}
我用来从数据上下文(数据容器?)是
this.GetEntity().Where(filterLambda)
.OrderBy(orderbyLambda)
.Skip(((int)page - 1) * (int)pageSize)
.Take((int)pageSize)
.Select(selectLambda).ToList();
需要
OrderBy()
.Skip()
和
.Take()
. 对于所有认为可以这样做的人来说,Linq to SQL是正确的。但是,Linq to实体不支持它:
The method 'Skip' is only supported for sorted input in LINQ to Entities.
The method 'OrderBy' must be called before the method 'Skip'.