¿Cómo tomo una DataTable y la convierto en una lista?DataTable to List <object>
He incluido algunos códigos a continuación, tanto en C# como en VB.NET, el problema con ambos es que creamos un nuevo objeto para devolver los datos, que es muy costoso. Necesito devolver una referencia al objeto.
El objeto DataSetNoteProcsTableAdapters.up_GetNoteRow implementa la interfaz INote.
estoy usando ADO.NET, junto con .NET 3.5
código C#
public static IList<INote> GetNotes()
{
DataSetNoteProcsTableAdapters.up_GetNoteTableAdapter adapter =
new DataSetNoteProcsTableAdapters.up_GetNoteTableAdapter();
DataSetNoteProcs.up_GetNoteDataTable table =
new DataSetNoteProcs.up_GetNoteDataTable();
IList<INote> notes = new List<INote>();
adapter.Connection = DataAccess.ConnectionSettings.Connection;
adapter.Fill(table);
foreach (DataSetNoteProcs.up_GetNoteRow t in table) {
notes.Add((INote)t);
}
return notes;
}
Código VB.NET
Public Shared Function GetNotes() As IList(Of INote)
Dim adapter As New DataSetNoteProcsTableAdapters.up_GetNoteTableAdapter
Dim table As New DataSetNoteProcs.up_GetNoteDataTable
Dim notes As IList(Of INote) = New List(Of INote)
adapter.Connection = DataAccess.ConnectionSettings.Connection
adapter.Fill(table)
For Each t As DataSetNoteProcs.up_GetNoteRow In table
notes.Add(CType(t, INote))
Next
Return notes
End Function
Cualquier razón por la cual no se está usando LINQ2SQL para esto? –
Dup: http://stackoverflow.com/questions/545328/datatable-to-generic-list-memory-leak/545429#545429 –
El cliente no quiere utilizar LINQ2SQL o LINQ en general. Gracias por el enlace, será de gran ayuda. Voy a ejecutar algunas veces y ver qué pasa – Coppermill