locked
order by multiple fields in generic repository RRS feed

  • Question

  • i have following method in my generic repository which can order by a field name. I need to know how can I get it to order by more than one field.

    public IList<TEntity> FindByExpression<TOrderKey>(Expression<Func<TEntity, bool>> filter,
                                                        Expression<Func<TEntity, object>> subSelector,
                                                        Expression<Func<TEntity, TOrderKey>> orderBy)
    {
        return SessionScope.Current.Set<TEntity>().Include(subSelector).Where(filter).OrderBy(orderBy).ToList();
    }

    thanks


    -= JL =-

    Saturday, February 2, 2013 1:42 AM

All replies

  • Hi Job Lot,

    Welcome to the MSDN forum.

    You can use ThenBy clause after OrderBy: http://msdn.microsoft.com/en-us/library/bb534743.aspx?

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 4, 2013 6:57 AM
  • Alexnader, how can pass those fields? I don't want to be using a comma separated list of fields as a string and splitting it before passing it to ThenBy.

    I tried following and getting casting error

    public IList<TEntity> FindByExpressionOrdered(Expression<Func<TEntity, bool>> filter, params Expression<Func<TEntity, object>>[] orderBy)
    {
        var query = SessionScope.Current.Set<TEntity>().Where(filter).OrderBy(orderBy.First());
        if (orderBy.Length > 1)
        {
            for (int i = 1; i < orderBy.Length; i++)
            {
                query = query.ThenBy(orderBy[i]);
            }
        }
        return query.ToList();
    }

    this is how i call the method

    IList<Product> products = IoC.Resolve<IRepository<Product>>().FindByExpressionOrdered(p => p.IsActive && p.IsFavorite, p => p.Name, p => p.Id);

    Thanks.

    • Edited by Job Lot Tuesday, February 5, 2013 11:03 PM
    Tuesday, February 5, 2013 10:43 PM
  • i have following method in my generic repository which can order by a field name. I need to know how can I get it to order by more than one field.

    public IList<TEntity> FindByExpressionOrdered(Expression<Func<TEntity, bool>> filter,
                                                    params Expression<Func<TEntity, object>>[] orderBy)
    {
        IOrderedQueryable<TEntity> query = SessionScope.Current.Set<TEntity>().Where(filter).OrderBy(orderBy.First());
        if (orderBy.Length > 1)
        {
            for (int i = 1; i < orderBy.Length; i++)
            {
                query = query.ThenBy(orderBy[i]);
            }
        }
        return query.ToList();
    }

    Calling the above as follows causes "Unable to cast the type 'System.Int64' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types." exception.

    IList<Product> prods = IoC.Resolve<IRepository<Product>>().FindByExpressionOrdered(p => p.IsActive && p.IsFavorite, p => p.Name, p => p.Id);


    -= JL =-

    Thursday, February 21, 2013 12:26 PM