Comment puis-je convertir un DataTable en liste?DataTable to List <object>
J'ai inclus du code ci-dessous à la fois dans C# et VB.NET, le problème avec ces deux choses est que nous créons un nouvel objet pour renvoyer les données, ce qui est très coûteux. Je dois retourner une référence à l'objet.
L'objet DataSetNoteProcsTableAdapters.up_GetNoteRow met en œuvre l'interface INote.
J'utilise ADO.NET, avec .NET 3.5
C# code
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;
}
code 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
Toute raison pour laquelle vous ne l'utilisez LINQ2SQL pour cela? –
Dup: http://stackoverflow.com/questions/545328/datatable-to-generic-list-memory-leak/545429#545429 –
Le client ne souhaite généralement pas utiliser LINQ2SQL ou LINQ. Merci pour le lien, ça va beaucoup aider. Je vais courir quelques fois et voir ce qui se passe – Coppermill