How to overcome foreach loop for list object dynamically

I'm swapping my values in List Object on some conditions and update List Object value.

Currently, what I'm doing is - Looping on each object through List - Check If condition is net - Swap values

public static void SwapMinMaxIfNull<T>(this IEnumerable<T> rows, string reportfor)
{
   if (reportfor.Equals("Comparison"))
   {
      var row = rows as IEnumerable<RiskBoardDataToExport>;                
      try
      {
         if (rows.Any())
         {
            var Tests = row.Where(min => min.MinGaitSpeed == null && min.MaxGaitSpeed != null).ToList();
            if (Tests != null)
            {
               foreach (RiskBoardDataToExport test in Tests)
               {
                  test.MinGaitSpeed = test.MaxGaitSpeed;
                  test.MaxGaitSpeed = null;
               }
            }
            // again check for next object 
            Tests = row.Where(min => min.MinTUGTime == null && min.MaxTUGTime != null).ToList();
            if (Tests != null)
            {
               foreach (RiskBoardDataToExport test in Tests)
               {
                  test.MinTUGTime = test.MaxTUGTime;
                  test.MaxTUGTime = null;
               }
            }
            // again check for next object
             Tests = row.Where(min => min.MinBergScoreSpeed == null && min.MaxBergScoreSpeed != null).ToList();
             if (Tests != null)
             {
                foreach (RiskBoardDataToExport test in Tests)
                {
                   test.MinBergScoreSpeed = test.MaxBergScoreSpeed;
                   test.MaxBergScoreSpeed = null;
                }
             }
             //.. for brevity
          }
     }
}

Can I do it in better way? I know about PropertyInfo i.e. Can check property name and get value etc, but, not having any hint to get this done.

Thanks

2 answers

  • answered 2019-05-22 06:01 Rufus L

    It's not exactly what you're asking for, but you can combine the clauses in your Where statements and then have a few if statements in the body:

    public static void SwapMinMaxIfNull(this IEnumerable<RiskBoardDataToExport> rows, 
        string reportfor)
    {
        if (rows = null) return;
    
        if (reportfor.Equals("Comparison", StringComparison.OrdinalIgnoreCase))
        {
            foreach (var row in rows.Where(r =>
                (r.MinGaitSpeed == null && r.MaxGaitSpeed != null) ||
                (r.MinBergScoreSpeed == null && r.MaxBergScoreSpeed != null) ||
                (r.MinBergScoreSpeed == null && r.MaxBergScoreSpeed != null)))
            {
                if (row.MinGaitSpeed == null)
                {
                    row.MinGaitSpeed = row.MaxGaitSpeed;
                    row.MaxGaitSpeed = null;
                }
                if (row.MinTUGTime == null)
                {
                    row.MinTUGTime = row.MaxTUGTime;
                    row.MaxTUGTime = null;
                }
                if (row.MinBergScoreSpeed == null)
                {
                    row.MinBergScoreSpeed = row.MaxBergScoreSpeed;
                    row.MaxBergScoreSpeed = null;
                }
            }
        }
    }
    

  • answered 2019-05-22 06:42 iSpain17

    As this is an operation where order of the items in the list does not matter, you can easily speed this up by parallelization (you can read up on that here).

    So, what you should do, is handle this foreach loop in a parallel way and combine it with Rufus L's optimized code for the fastest result.

    var rows = rows.Where(r =>
       (r.MinGaitSpeed == null && r.MaxGaitSpeed != null) ||
       (r.MinBergScoreSpeed == null && r.MaxBergScoreSpeed != null) ||
       (r.MinBergScoreSpeed == null && r.MaxBergScoreSpeed != null))
    
    Parallel.ForEach(rows, (row) => {
    {
       if (row.MinGaitSpeed == null)
       {
          row.MinGaitSpeed = row.MaxGaitSpeed;
          row.MaxGaitSpeed = null;
       }
       if (row.MinTUGTime == null)
       {
          row.MinTUGTime = row.MaxTUGTime;
          row.MaxTUGTime = null;
       }
       if (row.MinBergScoreSpeed == null)
       {
          row.MinBergScoreSpeed = row.MaxBergScoreSpeed;
          row.MaxBergScoreSpeed = null;
       }
    }
    

    Note that this requires the System.Threading.Tasks namespace, that's where the Parallel class is.