wybieranie elementów z listy - czy dobrze to napisałem

0

Tak więc wybieram elementy z listy , ale nie wiem jakiego te elementy są typu więc:

   List<DataGridViewRow> lista = new List<DataGridViewRow>(); // wybrane elementy

               if (granicznyElement.GetType()== typeof(DateTime))
               {
                   for (int i = 0; i < Rows.Count; i++)
                   { 
                       if ( ((DateTime)Rows[i].Cells[ktoraKolumna].Value) >= (DateTime)granicznyElement)
                       { lista.Add(Rows[i]); }
                   }
               }
               else if (granicznyElement.GetType() == typeof(int) || granicznyElement.GetType() == typeof(long))
               {
                   for (int i = 0; i < Rows.Count; i++)
                   {
                       if (((long)w.Rows[i].Cells[ktoraKolumna].Value) >= (long)granicznyElement)
                       { lista.Add(Rows[i]); }
                   }  
               }
               else if (granicznyElement.GetType() == typeof(decimal) || granicznyElement.GetType() == typeof(float))
               {
                   for (int i = 0; i < Rows.Count; i++)
                   {
                       if (((decimal)Rows[i].Cells[ktoraKolumna].Value) >= (decimal)granicznyElement)
                       { lista.Add(Rows[i]); }
                   } 
               } 
0

Ja to po pierwsze wywołałbym GetType() raz na początku metody i zapamiętał w zmiennej, a nie wołał 5 razy w pesymistycznym przypadku:

Type type = granicznyElement.GetType();

if (type == typeof(DateTime))
{
    //
}
else if (type == typeof(int) || type == typeof(long))
{
    //
}

A po drugie, wydaje mi się, że wystarczy Ci coś takiego:

 for (int i = 0; i < Rows.Count; i++)
{
    if (((IComparable)Rows[i].Cells[ktoraKolumna].Value).CompareTo((IComparable)granicznyElement) >= 0)
    {
        lista.Add(Rows[i]);
    }
}
0

Dzięki somekind :) .

1 użytkowników online, w tym zalogowanych: 0, gości: 1