使用DbSet应用OrderBy [英] Apply OrderBy with DbSet

查看:637
本文介绍了使用DbSet应用OrderBy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现分页,并与通用存储库进行排序。 ?如何采取主键列默认顺序由列DbSet

  DbSet = Context.Set< T>(); 

公众的IQueryable< T> GETALL(INT PAGENUMBER = 0,INT的pageSize = 10,串sortColumn =)
{
返回DbSet.OrderBy(如何在这里使用的主键列)。跳过(PAGENUMBER * pageSize的)取(pageSize的);
}


解决方案

我已经使用了这些扩展方法实现类似的东西:

 公共静态字符串GetKeyField(type类型)
{
VAR allProperties =类型.GetProperties();

VAR keyProperty = allProperties.SingleOrDefault(P => p.IsDefined(typeof运算(KeyAttribute)));

返回keyProperty!= NULL? keyProperty.Name:空;
}

公共静态的IQueryable< T>排序依据< T>(这IQueryable的< T>源,串排序依据)
{
返回source.GetOrderByQuery(排序依据,排序依据);
}

公共静态的IQueryable< T> OrderByDescending< T>(这IQueryable的< T>源,串排序依据)
{
返回source.GetOrderByQuery(排序依据,OrderByDescending);
}

私有静态的IQueryable< T> GetOrderByQuery< T>(这IQueryable的< T>源,串排序依据,字符串methodName中)
{
VAR sourceType的= typeof运算(T);
VAR财产= sourceType.GetProperty(排序依据);
变种parameterExpression = Expression.Parameter(sourceType的,X);
VAR getPropertyExpression = Expression.MakeMemberAccess(parameterExpression,属性);
VAR orderByExpression = Expression.Lambda(getPropertyExpression,parameterExpression);
VAR resultExpression = Expression.Call(typeof运算(可查询),方法名,
新的[] {sourceType的,property.PropertyType},source.Expression,
orderByExpression);

返回source.Provider.CreateQuery< T>(resultExpression);
}

这允许你通过属性名作为一个字符串,并建立一个表达它传递到正规的LINQ的OrderBy()函数。所以你的情况,使用情况将是:



  DbSet = Context.Set< T>(); 

公众的IQueryable< T> GETALL(INT PAGENUMBER = 0,INT的pageSize = 10,串sortColumn =)
{
返回DbSet.OrderBy(GetKeyField(typeof运算(T)))跳过(PAGENUMBER * pageSize的)采取(pageSize的);
}

这假定您的关键领域中的实体类装饰正确地与属性。


I am trying to implement pagination and sorting with generic repository. How to take primary key column as default order by column in DbSet ?

DbSet = Context.Set<T>();

public IQueryable<T> GetAll(int pageNumber = 0, int pageSize = 10, string sortColumn = "")
{
    return DbSet.OrderBy("how to use primary key column here").Skip(pageNumber * pageSize)Take(pageSize);
}

解决方案

I have used these extension methods to achieve something similar:

public static string GetKeyField(Type type)
{
    var allProperties = type.GetProperties();

    var keyProperty = allProperties.SingleOrDefault(p => p.IsDefined(typeof(KeyAttribute)));

    return keyProperty != null ? keyProperty.Name : null;
}

public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string orderBy)
{
    return source.GetOrderByQuery(orderBy, "OrderBy");
}

public static IQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string orderBy)
{
    return source.GetOrderByQuery(orderBy, "OrderByDescending");
}

private static IQueryable<T> GetOrderByQuery<T>(this IQueryable<T> source, string orderBy, string methodName)
    {
        var sourceType = typeof(T);
        var property = sourceType.GetProperty(orderBy);
        var parameterExpression = Expression.Parameter(sourceType, "x");
        var getPropertyExpression = Expression.MakeMemberAccess(parameterExpression, property);
        var orderByExpression = Expression.Lambda(getPropertyExpression, parameterExpression);
        var resultExpression = Expression.Call(typeof(Queryable), methodName,
                                               new[] { sourceType, property.PropertyType }, source.Expression,
                                               orderByExpression);

        return source.Provider.CreateQuery<T>(resultExpression);
    }

This allows you to pass the property name as a string and build up an expression which it passes to the regular LINQ OrderBy() function. So in your case, the usage would be:

DbSet = Context.Set<T>();

public IQueryable<T> GetAll(int pageNumber = 0, int pageSize = 10, string sortColumn = "")
{
    return DbSet.OrderBy(GetKeyField(typeof(T))).Skip(pageNumber * pageSize)Take(pageSize);
}

This assumes your key field in your entity class is properly decorated with the Key attribute.

这篇关于使用DbSet应用OrderBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆