Come si acquisisce un DataTable e lo si converte in un elenco?DataTable to List <object>
Ho incluso del codice sotto sia in C# che in VB.NET, il problema con entrambi è che creiamo un nuovo oggetto per restituire i dati, che è molto costoso. Devo restituire un riferimento all'oggetto.
L'oggetto DataSetNoteProcsTableAdapters.up_GetNoteRow implementa l'interfaccia INote.
Sto usando ADO.NET, insieme a .NET 3.5
codice 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;
}
VB.NET Codice
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
Qual è il motivo per cui non lo si utilizza LINQ2SQL per questo? –
Dup: http://stackoverflow.com/questions/545328/datatable-to-generic-list-memory-leak/545429#545429 –
Il cliente non desidera utilizzare LINQ2SQL o LINQ in generale. Grazie per il collegamento, sarà di grande aiuto. Ho intenzione di correre qualche volta e vedere cosa succede – Coppermill