将数据集代码VB.net转换为C#

时间:2015-10-14 18:12:15

标签: c# vb.net

我有以下VB.net代码:

Private Function ReorgFileDataset(ByRef InDataSet As DataSet) As DataSet
  Dim _ResultDataRow, _DataRow As DataRow
  Dim _ResultDataSet As New DataSet
  Dim _ResultDataTable As New DataTable
  _ResultDataSet.Tables.Add(_ResultDataTable)
  Dim ColumnName, Value As String
  Dim FileID As Integer
  For Each _DataRow In InDataSet.Tables(0).Rows
     ColumnName = _DataRow.Item("FKP_KEYWORD")
     If Not _ResultDataTable.Columns.Contains(ColumnName) Then
        _ResultDataTable.Columns.Add(ColumnName)
     End If
  Next
  _ResultDataTable.Columns.Add("DATE_SENT")

  _ResultDataRow = Nothing
  For Each _DataRow In InDataSet.Tables(0).Rows
     If FileID <> _DataRow.Item("FD_RID") Then
        If Not _ResultDataRow Is Nothing Then
           _ResultDataTable.Rows.Add(_ResultDataRow)
        End If
        FileID = _DataRow.Item("FD_RID")
        _ResultDataRow = _ResultDataTable.NewRow()
        _ResultDataRow.Item("DATE_SENT") = _DataRow.Item("FD_LASTMODIFIED")
     End If
     If _DataRow.Item("FKP_KEYWORD").GetType IsNot GetType(DBNull) And _
        _DataRow.Item("FKP_VALUE").GetType IsNot GetType(DBNull) Then
        ColumnName = _DataRow.Item("FKP_KEYWORD")
        Value = _DataRow.Item("FKP_VALUE")
        _ResultDataRow.Item(ColumnName) = Value
     End If
  Next
  If _ResultDataRow IsNot Nothing Then
     _ResultDataTable.Rows.Add(_ResultDataRow)
  End If

  Return _ResultDataSet
End Function

当我通过转换器运行它时,我得到了这个C#代码:

private DataSet ReorgFileDataset(DataSet InDataSet)
{
    DataRow _ResultDataRow = default(DataRow);
    DataSet _ResultDataSet = new DataSet();
    DataTable _ResultDataTable = new DataTable();
    _ResultDataSet.Tables.Add(_ResultDataTable);
    string ColumnName = null;
    string Value = null;
    int FileID = 0;


    foreach (DataRow _DataRow in InDataSet.Tables[0].Rows) {
        ColumnName = _DataRow.Item("FKP_KEYWORD");
        if (!_ResultDataTable.Columns.Contains(ColumnName)) {
            _ResultDataTable.Columns.Add(ColumnName);
        }
    }
    _ResultDataTable.Columns.Add("DATE_SENT");

    _ResultDataRow = null;
    foreach (DataRow _DataRow in InDataSet.Tables[0].Rows) {
        if (FileID != _DataRow.Item("FD_RID")) {
            if ((_ResultDataRow != null)) {
                _ResultDataTable.Rows.Add(_ResultDataRow);
            }
            FileID = _DataRow.Item("FD_RID");
            _ResultDataRow = _ResultDataTable.NewRow();
            _ResultDataRow.Item("DATE_SENT") = _DataRow.Item("FD_LASTMODIFIED");
        }
        if (!object.ReferenceEquals(_DataRow.Item("FKP_KEYWORD").GetType, typeof(DBNull)) & !object.ReferenceEquals(_DataRow.Item("FKP_VALUE").GetType, typeof(DBNull))) {
            ColumnName = _DataRow.Item("FKP_KEYWORD");
            Value = _DataRow.Item("FKP_VALUE");
            _ResultDataRow.Item(ColumnName) = Value;
        }
    }
    if (_ResultDataRow != null) {
        _ResultDataTable.Rows.Add(_ResultDataRow);
    }

    return _ResultDataSet;
}

我将InDataSet.Tables(0)修改为InDataSet.Tables[0]但是,由于intellisence没有DataRow的Item属性,因此我无法修复多个错误。我也不确定它在检查Nulls的最后一个if语句。

我可以在这里得到一些帮助吗?

1 个答案:

答案 0 :(得分:0)

这篇SO帖子几乎是重复的......

In C#: Why no 'Item' on System.Data.DataRow?

更具体地说,您可以使用索引器访问数据行的列。例如,您有这行代码......

ColumnName = _DataRow.Item("FKP_KEYWORD");

您可以将其更改为此...

ColumnName = _DataRow["FKP_KEYWORD"].ToString();