Sort by Reflection with GetProperty

0

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

However, giving error saying that it does not recognize the Reflection GetProperty method, since lambda can not translate.

How can I do this sort order logic?

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

    return obj.Pagination();
}

Follow the image of the error:

----Edit----

Refactoring(rsrs)myprobleminasummarizedwayisthatIwantaService,whatever,whenIwanttomakeanappointment,evenifitdoesnotpassanyparameters,theprogramalreadylimitswithpagination,goodpracticesunderstandmeright?!BelowaresomepointstounderstandandintheendIputthepastebinlinktoviewtheclassesExtensionsQueries.cs,Service.csandClientService.cs.Ijustputsomeoftheclasses,Ihopethat'senough.

  • IhaveagenericclassformyServicestoinheritwhereitcontainsmethodscommontoallServices,suchasGetAll(),GetBy(Expression<Func<TEntity,bool>>filter),Insert(TEntityobj),etc...;

  • TEntityisagenericDatabaseMappedEntitythatthisClassServicewilluse;

Sofareverythingisworkingperfect,nowIstartedtoimplementaformofpaginationthatisgenerictoo,soIstartedtomodifysomeclassestoadptarandthat'swhereIhadthisproblem,Istarteddoingthefollowing:

    IcreatedanIQuerableExtensionMethodtodothereturnpagingofaquery;

  • TheproblemisthatSkiponlyworksifthereisanorderingbefore;

  • SinceI'mgoingtohavetoimplementanordering,I'mwantingtoparameterizetheGetAll()andtheothersthatwillneedandgetsomeattributetobeusedinordering,typeGetAll(objparamsToOrder),butifitdoesnotitwillbemandatory)tosortbytheKeyPrimaria,whichinmycaseistheNameoftheClasswiththeIdattheend(whichwaswhereIstartedtoimplementasseenintheexampleIposted);

  • Idonotevenknowhowtopasstheseparameters,becausetheideaistopassNattributestoorderandeachhavetheirsortorderASCorDESC.ButbeforeIgottothatpart,Ialreadyhadthisproblem.T_T

Link: link

    
asked by anonymous 24.06.2017 / 07:41

1 answer

0

Have you ever tried to do this?

_repository.GetAll () OrderByDescending (x => Convert.ToInt32 (x.GetType ()) GetProperty ("Id") GetValue (x, null) .ToString ())) ToList ();

Here it sorts the objects by ID in descending order. If you want to grow to OrderBy

    
19.07.2018 / 21:02