DataSetNoteProcsTableAdapters

DataSetNoteProcsTableAdapters

本文介绍了DataTable to List< object>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用DataTable并将其转换为列表?



我在C#和VB.NET中都包含了一些代码,这两个问题这些是我们创建一个新对象来返回数据,这是非常昂贵的。我需要返回对象的引用。



DataSetNoteProcsTableAdapters.up_GetNoteRow对象确实实现了INote接口。



我正在使用ADO.NET,连同.NET 3.5



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);
}

返回笔记;
}

VB.NET代码



公共共享函数GetNotes()作为IList(Of INote)
Dim适配器作为新的DataSetNoteProcsTableAdapters.up_GetNoteTableAdapter
Dim table As New DataSetNoteProcs.up_GetNoteDataTable

Dim notes As IList(Of INote)=新列表(INote)

adapter.Connection = DataAccess.ConnectionSettings.Connection
adapter.Fill(table)

对于每个t作为DataSetNoteProcs.up_GetNoteRow在表
notes.Add(CType(t,INote))
下一个

返回注释
结束函数


解决方案

不,创建列表并不昂贵。与创建数据表并从数据库中获取数据相比,它非常便宜。



您可以通过在填充表后创建列表使其更便宜,以便您可以将初始容量设置为您将放入的行数:

  IList< INote> notes = new List< INote>(table.Rows.Count); 


How do I take a DataTable and convert it to a List?

I've included some code below in both C# and VB.NET, the issue with both of these is that we create a new object to return the data, which is very costly. I need to return a reference to the object.

The DataSetNoteProcsTableAdapters.up_GetNoteRow object does implement the INote interface.

I am using ADO.NET, along with .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;
}

VB.NET Code

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
解决方案

No, creating a list is not costly. Compared to creating the data table and fetching the data from the database, it's very cheap.

You can make it even cheaper by creating the list after populating the table, so that you can set the initial capacity to the number of rows that you will put in it:

IList<INote> notes = new List<INote>(table.Rows.Count);

这篇关于DataTable to List&lt; object&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:39