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,suchas
GetAll(),GetBy(Expression<Func<TEntity,bool>>filter),Insert(TEntityobj)
,etc...;TEntityisagenericDatabaseMappedEntitythatthisClassServicewilluse;
Sofareverythingisworkingperfect,nowIstartedtoimplementaformofpaginationthatisgenerictoo,soIstartedtomodifysomeclassestoadptarandthat'swhereIhadthisproblem,Istarteddoingthefollowing:
TheproblemisthatSkiponlyworksifthereisanorderingbefore;
SinceI'mgoingtohavetoimplementanordering,I'mwantingtoparameterizetheGetAll()andtheothersthatwillneedandgetsomeattributetobeusedinordering,typeGetAll(objparamsToOrder),butifitdoesnotitwillbemandatory)tosortbytheKeyPrimaria,whichinmycaseistheNameoftheClasswiththeIdattheend(whichwaswhereIstartedtoimplementasseenintheexampleIposted);
Idonotevenknowhowtopasstheseparameters,becausetheideaistopassNattributestoorderandeachhavetheirsortorderASCorDESC.ButbeforeIgottothatpart,Ialreadyhadthisproblem.T_T
IcreatedanIQuerableExtensionMethodtodothereturnpagingofaquery;
Link: link