Vaso Beruashvili
Vaso Beruashvili

Reputation: 677

C# OrderBy IQueryable<T> with Reflection by Property with attribute named For example "Key"

Is it possible to in C# to OrderBy IQueryable with Reflection to get Property name for ordering by property attribute for example Attribute Name = "Key"?

Upvotes: 1

Views: 412

Answers (1)

Vaso Beruashvili
Vaso Beruashvili

Reputation: 677

Resolved with: System.Linq.Dynamic enter link description here

var keyPropertyName = typeof(TEntity).GetProperties()
    .First(p => p.CustomAttributes.Any(ca => ca.AttributeType.Name == "KeyAttribute")).Name;

return _dbSet.OrderBy(keyPropertyName).Skip(skip).Take(take).ToList();

Upvotes: 2

Related Questions