简体   繁体   中英

C#: DataTable conversion row-by-row

In an abstract class ( C# 3 ), there is a virtual method named GetData which returns a DataTable. The algorithm of the method is as following:

  1. Get SQL query string from the class.
  2. Get DataTable from database using above query.
  3. Do transformations on DataTable and return it.

In the 3rd point, I clone the original DataTable in order to change the Type of column (I can't do that on populated table and can't setup this at the 2nd point) and enumerate every row in which I copy and transform data from the original one. Transformations depends on the class, every one has private methods which transforms data on its own.

The question is: How to make a method in a base class which will be based on the following three params: column name which will be converted, Type of new column and action during the transformation. Two first are quite simple but I'm not sure about the third one. I thought about designing a new class which will store these three parameters, and the action will be stored as following delegate:

public delegate object Convert(object objectToConvert);

The conversion would look something like that:

int rowCounter = 0;

foreach(DataRow row in dt.Rows)
{
 foreach(var item in Params)
 {
  row[item.ColumnIndex] = item.Convert(originalDataTable.Rows[rowCounter].ItemArray[item.ColumnIndex]);
 }

 ++rowCounter;
}

For now, I have to override the method GetData() in every class I want to have a transformations which causes a large duplication of code. The point I want to achieve is to make a base class which will be based on params I mentioned above. Is the above solution good for this problem or is it any other way to do that?

First I'm not a big fan of DataTable, you can use DataReader instead if you do a foreach.

using (var dataReader = ....)
    {
         while(dataReader.Read())
         {
             foreach(var item in Params)
             {
                  row[item.ColumnIndex] = item.Convert(originalDataTable.Rows[rowCounter].ItemArray[item.ColumnIndex]);
             }
         }
    }

Second, make a base class with a abstract method with some code and make the object in the list Params inherits of this class and override the method only when it necessary.

public class MyClass
{
      public abstract object Convert(DataRow row)
      {
      ....
      }
}

public class foo : MyClass
{
}

Okay, for now I have a following solution which does fully satisfy me:

Every convertor implements following interface:

public interface IConvertor
{
 object Convert(object item);
}

Params class is as following:

Type ColumnType { get; protected set; } // New type
string[] ColumnNames { get; protected set; }
IConvertor Convertor { get; protected set; }

Every object have its own Params array property. I've got one method for every derived class and all what I have to do is to set up parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM