I am trying to convert C# code to VB.Net but unable to find solution yield return equivalent of C# in Vb.NET
I am iterating over the datatable. One approach which I have found is to use list to iterate over the Datatable rows. But again it requires to convert the list to datatable. Hence what approach is required to yield return the datatable in VB.NET?
Here is VB.NET my Code:
Public Function GetFileData(ByVal sourceFileFullName As String, ByVal dt1 As System.Data.DataTable, ByVal RowCount As Integer) As IEnumerable(Of System.Data.DataTable)
Dim con As New SqlConnection
Dim cmd As New SqlCommand
con.ConnectionString = Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings("con1").ConnectionString)
Dim chunkRowCount As Integer = 0
Dim Row As String
Using sr As StreamReader = New StreamReader(sourceFileFullName)
While Not (Row = sr.ReadLine()) = ""
If Not RowCount = 0 Then
chunkRowCount = chunkRowCount + 1
//var chunkDataTable = ; //Code for filling datatable or whatever
dt1.Rows.Add()
Dim i As Integer = 0
Dim Cell As String
For Each Cell in Row.Split(',')
If (String.IsNullOrEmpty(Cell)) Then
dt1.Rows(dt1.Rows.Count - 1)(i) = DBNull.Value
i = i + 1
ElseIf Cell = "00.00.0000" Then
dt1.Rows(dt1.Rows.Count - 1)(i) = DBNull.Value
i = i + 1
Else
dt1.Rows(dt1.Rows.Count - 1)(i) = Cell
i = i + 1
End If
Next
End If
RowCount = RowCount + 1
If chunkRowCount = 10000 Then
chunkRowCount = 0
Yield return dt1
dt1.Clear()
End If
End While
End Using
If dt1.Rows.Count > 0 Then
yield return dt1
End If
End Function
C# Code:
public static IEnumerable<System.Data.DataTable> GetFileData(string sourceFileFullName, System.Data.DataTable dt1, int RowCount)
{
var con = ConfigurationManager.ConnectionStrings["con1"].ConnectionString.ToString();
var connection = new SqlConnection(con);
int chunkRowCount = 0;
string Row;
using (var sr = new StreamReader(sourceFileFullName))
{
//Read and display lines from the file until the end of the file is reached.
while ((Row = sr.ReadLine()) != null)
{
if (RowCount != 0)
{
chunkRowCount++;
//var chunkDataTable = ; //Code for filling datatable or whatever
dt1.Rows.Add();
int i = 0;
foreach (string Cell in Row.Split(','))
{
if (String.IsNullOrEmpty(Cell))
{
dt1.Rows[dt1.Rows.Count - 1][i] = DBNull.Value;
i = i + 1;
}
else if (Cell == "00.00.0000")
{
dt1.Rows[dt1.Rows.Count - 1][i] = DBNull.Value;
i = i + 1;
}
else
{
dt1.Rows[dt1.Rows.Count - 1][i] = Cell;
i = i + 1;
}
}
}
RowCount = RowCount + 1;
if (chunkRowCount == 10000)
{
chunkRowCount = 0;
yield return dt1;
dt1.Clear(); // = null;
}
} //end while
}
//return last set of data which less then chunk size
if (dt1.Rows.Count > 0)
yield return dt1;
}