Search code examples
c#visual-studioreflectionblazor-server-side

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.

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();