tags:

views:

425

answers:

1

Using dotnet 2.0. I currently have code like this :

  DataView dv = new DataView(dsTrans.Transactions, filterSpec, sortSpec, DataViewRowState.CurrentRows);

  foreach (DataRowView dvr in dv)
  {
   DSTransactions.TransactionsRow transRow = (DSTransactions.TransactionsRow)dvr.Row;
   // do something with transRow
  }

where "dsTrans" is a strongly typed DataSet. I'm wondering if there is a more type-safe way to iterate over the rows of the DataView, which does not involve using a cast (or using the "as" keyword).

(Note that some ordering and filtering is needed, which is why a DataView is used)

Thanks.

+1  A: 

You could try this:

        // Only one cast here
        Enumerator<DSTransactions.TransactionsRow> enumer = (IEnumerator<DSTransactions.TransactionsRow>)dv.GetEnumerator();
        while (enumer.MoveNext())
        {
            // enumer.Current will be of type DSTransactions.TransactionsRow
            Console.WriteLine(enumer.Current);
        }
        enumer.Dispose();
SwDevMan81