I have a collection variable, whose elements are dynamic type, that is returned from a database using Dapper extension.
The returned collection is this (variable name is reporte):
[0]: {{DapperRow, centroCosto = '(sin centro de costo)', Almuerzo = '20', Cena = '11', Desayuno = '2', Once = '3', servicios = '36', valorTotal = '106800,00'}}
[1]: {{DapperRow, centroCosto = 'ASESOR', Almuerzo = '18', Cena = '0', Desayuno = '0', Once = '0', servicios = '18', valorTotal = '55800,00'}}
[2]: {{DapperRow, centroCosto = 'AUXILIAR DE ASEO', Almuerzo = '6', Cena = '10', Desayuno = '5', Once = '6', servicios = '27', valorTotal = '70300,00'}}
[3]: {{DapperRow, centroCosto = 'DEMO', Almuerzo = '1437', Cena = '669', Desayuno = '57', Once = '71', servicios = '2234', valorTotal = '6880000,00'}}
[4]: {{DapperRow, centroCosto = 'INFORMÁTICA', Almuerzo = '4', Cena = '0', Desayuno = '0', Once = '0', servicios = '4', valorTotal = '12400,00'}}
[5]: {{DapperRow, centroCosto = 'PRACTICA DEMO', Almuerzo = '20', Cena = '0', Desayuno = '0', Once = '0', servicios = '20', valorTotal = '62000,00'}}
[6]: {{DapperRow, centroCosto = 'PRODEMO', Almuerzo = '81', Cena = '92', Desayuno = '2', Once = '3', servicios = '178', valorTotal = '563200,00'}}
From that list, I tell you that centroCosto, servicios and valorTotal fields are known and fixed, so, I can order that collection by any of those fields using reporte.OrderBy(r => r.centroCosto), reporte.OrderBy(r => r.servicios) and reporte.OrderBy(r => r.valorTotal).
Other fields are dynamic and I don't know their names in advance (that is why I used Dapper).
How can I order by those fields? For example, by Almuerzo field?
If you convert the list object to idictionary to fetch the column name for sorting by orderby and then convert back to list. it is possible that you can sort the whole list and then you can use it with your dto to pass the list back to the client as:
For example, your list name is
ulist.You can write it as:Where
ris an object of the list andcolumnNameis the corresponding entity of that object.