Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# reflection to get the field-object into C# code from a field-name?

I have a coding question, thinking that Reflection may be the way to satisfy a C# VS-2022 source code situation.

My question is after reading below…Can Reflection be used to simplify the hard-coded field-names for ordering a list?

I have this sentence where the r.TXT_DISPL_VEHICLE code is obviously typed into the source code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

case 1:
// For the first sort-box, ALWAYS sort by the 'DTTM_TRIP_START_DATE'.
_ListForGrid = _ListForGrid.AsQueryable().OrderBy(r => r.DTTM_TRIP_START_DATE)
               .ThenByDescending(r => r.TXT_DISPL_VEHICLE).ToList();

However, this field-name TXT_DISPL_VEHICLE is based on which class-name is being used for the ordering sentence. We have a number of items that call this function, such as:

class-name          field-name
----------------    --------------------
"Vehicle"       ,   "TXT_DISPL_VEHICLE"         
"Department"    ,   "TXT_DEPT_ABBREV"               
"Driver"        ,   "TXT_DISPL_DRIVER_NAME"     
"Manager"       ,   "TXT_DISPL_DRIVER_NAME" 

I am wondering if Reflection could solve the coding problem of avoiding a series of switch-statements such as:

switch (itemType) {
case "Vehicle":
//use the value TXT_DISPL_VEHICLE in the code for vehicle.
break;

… and similar for each item.

Furthermore, the "ordering" sentences get more complex and are based on how many items are being used in the OrderBy clause…for example the third type of ordering…

case 3:
// For the first sort-box, ALWAYS sort by the 'DTTM_TRIP_START_DATE'.
if (saOrderBys[0] == "DESC") {
    int soValue13 = Convert.ToInt32(ddlOrderByFieldNamesList.FirstOrDefault(r => r.Key == iaSortDDLs[0]).Value);
    _ListForGrid = _ListForGrid.AsQueryable().OrderByDescending(r => r.DTTM_TRIP_START_DATE)
        .ThenBy(r => r.TXT_DISPL_VEHICLE)
        .ThenBy(r => r.TXT_DISPL_DRIVER_NAME).ToList();
} else if (iDESC == 2) {
    _ListForGrid = _ListForGrid.AsQueryable().OrderBy(r => r.DTTM_TRIP_START_DATE)
        .ThenByDescending(r => r.TXT_DISPL_VEHICLE)
        .ThenBy(r => r.TXT_DISPL_DRIVER_NAME).ToList();
} else if (iDESC == 3) {
    _ListForGrid = _ListForGrid.AsQueryable().OrderBy(r => r.DTTM_TRIP_START_DATE)
        .ThenBy(r => r.DTTM_TRIP_START_DATE)
        .ThenByDescending(r => r.TXT_DISPL_DRIVER_NAME).ToList();
}
return;

>Solution :

I would suggest using new switch expression, it would result in simplified syntax:

// For the first sort-box, ALWAYS sort by the 'DTTM_TRIP_START_DATE'.
_ListForGrid = _ListForGrid
    .AsQueryable()
    .OrderBy(r => r.DTTM_TRIP_START_DATE)
    .ThenByDescending(r => itemType switch
    {
        "Vehicle" => r.TXT_DISPL_VEHICLE,
        "Department" => r.TXT_DEPT_ABBREV,
        "Driver" => r.TXT_DISPL_DRIVER_NAME,
        "Manager" => r.TXT_DISPL_DRIVER_NAME,
    })
    .ThenBy(r => r.TXT_DISPL_DRIVER_NAME)
    .ToList();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading