Ananda G
Ananda G

Reputation: 2539

How to sort upon <TEntity> unknown dynamic generic List?

Upadate:

I have prepared a generic function for capturing database rows for generation the listing. My method is exactly like this.

    public class GenericDataAccess<TEntity> : IGenericRepository<TEntity> where TEntity : class
    {
        internal ELearningDBContext ELearningDBContext;
        internal DbSet<TEntity> ELearningDBSet;

        public GenericDataAccess(ELearningDBContext context)
        {
            this.ELearningDBContext = context;
            this.ELearningDBSet = context.Set<TEntity>();
        }
        public virtual PagingModel<TEntity> GetAllPaged(int pagesize, NameValueCollection queryString)
        {
            return ELearningDBSet.AsQueryable().ToList();
        }
     }

Here I am facing a problem to sort this list. Because the TEntity is changing time to time. However, I have a common field in every Entity named Id. So I want to sort each and every model by Id in descending order.

I have tried this way. But it was not effective, moreover, it's generating an exception.

ELearningDBSet.AsQueryable()..OrderByDescending(i=>typeof(TEntity).GetProperty("Id")).ToList();

I have gone through these questions Q1, Q2, Q3, Q4. However, these were not effective. I prefer C# code. Any kind of perfect solution is highly appreciated. Thank you.

Upvotes: 2

Views: 175

Answers (3)

aaronR
aaronR

Reputation: 1567

Create an interface for all of your data entities to implement

public interface IEntityBase
{
    long Id { get; set; }
}

Then in your data class, in your case TEntity, Implement the IEntityBase interface.

public partial class MyTEntity: IEntityBase
{
    public long Id { get; set; }

    //Other attributes as needed.
}

In your generic class GenericDataAccess<TEntity> add a constraint to make TEntity implement the new interface IEntityBase. Then in the GetAllPaged method add the OrderByDescending() method call using the IEntityBase.Id property.

public class GenericDataAccess<TEntity> : IGenericRepository<TEntity> where TEntity : class, IEntityBase
    {
        internal ELearningDBContext ELearningDBContext;
        internal DbSet<TEntity> ELearningDBSet;

        public GenericDataAccess(ELearningDBContext context)
        {
            this.ELearningDBContext = context;
            this.ELearningDBSet = context.Set<TEntity>();
        }
        public virtual PagingModel<TEntity> GetAllPaged(int pagesize, NameValueCollection queryString)
        {
            return ELearningDBSet.AsQueryable().OrderByDescending(o => o.Id).ToList();
        }
     }

UPDATED: Added the partial descriptor for the MyEntityclass.

Upvotes: 2

Ananda G
Ananda G

Reputation: 2539

Wow!! I have just got a wonderful solution. I have just used it.

 ELearningDBSet.AsQueryable().SortBy("Id" + " Desc").ToList();

Here the SortBy() function I used, got it from the .Net Library named System.Web.UI.WebControls.QueryExtensions. For more details see here. Really it was a wonderful and outstanding solution for me.

Upvotes: 2

ASpirin
ASpirin

Reputation: 3651

You can build an Expression tree yourself e.g.

public static IQueryable<T> Sort<T>(this IQueryable<T> source, string field)
{
    var p = Expression.Parameter(typeof(T));
    var exp = Expression.Property(p, field);
    return source.OrderBy(Expression.Lambda<Func<T, object>>(exp, p));
}

Upvotes: 1

Related Questions