Sort by Reflection with Getproperty

Asked

Viewed 93 times

0

I have a generic class with a method that needs to sort a generic object.

However, give error saying that does not recognize the Reflection Getproperty method, because lambda can not translate.

How can I make this generic ordering logic?

public IEnumerable<TEntity> GetAll()
{
    var obj = _repository.GetAll()
        .OrderByDescending(x => x.GetType().GetProperty(typeof(TEntity).Name + "Id"));

    return obj.Pagination();
}

Follow the error screenshot:

inserir a descrição da imagem aqui

---- Edit ----

Refactoring (rsrs) my problem in summary form is that I want a Service, whatever, when you want to make a query, even if you do not pass any parameter, the program already limit with paging, good practices understand me right! Below a few points to understand and at the end I put the Pastebin link to view the classes Extensionsqueries.Cs, Service.Cs and Clienteservice.cs. I only put part of the classes, I hope it’s enough.

  • I have a generic class for my Services to inherit where it contains the methods common to all Services, such as GetAll(), GetBy(Expression<Func<TEntity, bool>> filter), Insert(TEntity obj), etc....;

  • Tentity is a generic Database Mapped Entity that this Service class will use;

So far everything is working perfect, now I started to implement a form of pagination that is generic tbm, so I started to modify some classes to adapt and that’s when I had this problem, I started to do the following:

  • I created an Extension Method for Iquerable to Paginate the Return of a Query;

  • The problem is that Skip only works if there is a Sort before;

  • Since I will have to implement an Ordination, I want to parameterize Getall() and the others that will need and receive some attribute to be used in the ordination, type Getall(obj paramsToOrder), but if it does not pass (it will not be mandatory) to sort by Chaveprimaria, which in my case is the Class Name with Id at the end (which is where I started to implement as you saw in the example I posted);

  • I don’t even know yet how to pass these parameters, because the idea is to pass N attributes to order and each to have their own sort of ASC or DESC sort. But before I got to that part I had this problem. T_T

Link: https://pastebin.com/ZiyjiurL

  • 1

    Why you need to use Reflection? Why not use OrderByDescending(x => x.Id)?

  • 2

    @There is always a fashion in abuses. Although the exception is still the greatest abuse, reflection is becoming strong now. It seems that it is becoming fashionable to use reflection by using, without any need and benefit. Reflection with a literal is always a "mistake", it is only useful when you do not know what you want to use. The other day I gave an answer about this and it was confusing, because when the person decides that he is going to use the mechanism, no one takes it out of his head :) I hope in this case it is different. As L2E uses reflection it becomes quite complicated to function reflection upon reflection.

  • 2

    If it were the case that it was useful to use, if I didn’t really know the name of the field, I would have to do the reflection outside to take the name and then use this in the query. Or use SQL itself :)

  • @moustache my question arises exactly for that fact: to be known not only the type as the name of the field. By the way, give me the link to your reply.

  • possibly you called this method, informing the TEntity as a string, then he tried to call GetProperty(typeof(string)). Put the class declaration where this method is also

1 answer

0

You’ve tried it this way?

_Getall(). Orderbydescending(x => Convert.Toint32(x.Gettype().Getproperty("Id").Getvalue(x, null).Tostring()). Tolist();

Here it sorts objects by Id in a decreasing way. If you want to crescent change to Orderby

Browser other questions tagged

You are not signed in. Login or sign up in order to post.