DataTable to List
如何获取DataTable并将其转换为List?
我在C#和VB.NET中都包含了一些代码,这两个问题都是我们创建一个新对象来返回数据,这是非常昂贵的。 我需要返回对象的引用。
DataSetNoteProcsTableAdapters.up_GetNoteRow对象确实实现了INote接口。
我正在使用ADO.NET和.NET 3.5
c#代码
public static IList GetNotes() { DataSetNoteProcsTableAdapters.up_GetNoteTableAdapter adapter = new DataSetNoteProcsTableAdapters.up_GetNoteTableAdapter(); DataSetNoteProcs.up_GetNoteDataTable table = new DataSetNoteProcs.up_GetNoteDataTable(); IList notes = new List(); adapter.Connection = DataAccess.ConnectionSettings.Connection; adapter.Fill(table); foreach (DataSetNoteProcs.up_GetNoteRow t in table) { notes.Add((INote)t); } return notes; }
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
不,创建列表并不昂贵。 与创建数据表和从数据库中获取数据相比,它非常便宜。
您可以通过在填充表格后创建列表来使其更便宜,这样您就可以将初始容量设置为您将放入其中的行数:
IList notes = new List (table.Rows.Count);
我有另一种方法可能值得一看。 这是一种帮助方法。 创建名为CollectionHelper的自定义类文件:
public static IList ConvertTo (DataTable table) { if (table == null) return null; List rows = new List (); foreach (DataRow row in table.Rows) rows.Add(row); return ConvertTo(rows); }
想象一下,你想获得一份客户名单。 现在您将拥有以下来电者:
List myList = (List )CollectionHelper.ConvertTo (table);
您在DataTable中拥有的属性必须与您的Customer类(名称,地址,电话等字段)匹配。
我希望它有所帮助!
谁愿意知道为什么要使用列表而不是DataTables: 链接文本
完整样本:
为什么不将DataTable传递给函数而不是实例化它? 这只会包含一个参考。
这太简单了,对你来说也是值得的,我敢肯定,但我不知道它是如何解决你的问题的。
不确定这是否是你想要的,但你可以尝试这样的事情。
public class Category { private int _Id; public int Id { get { return _Id; } set { _Id = value; } } private string _Name = null; public string Name { get { return _Name; } set { _Name = value; } } public Category() {} public static List GetCategories() { List currentCategories = new List (); DbCommand comm = GenericDataAccess.CreateTextCommand(); comm.CommandText = "SELECT Id, Name FROM Categories Order By Name"; DataTable table = GenericDataAccess.ExecuteSelectCommand(comm); foreach (DataRow row in table.Rows) { Category cat = new Category(); cat.Id = int.Parse(row["Id"].ToString()); cat.Name = row["Name"].ToString(); currentCategories.Add(cat); } return currentCategories; } }
这就是我所做的,所以希望这会有所帮助。 不确定它是否是正确的方法,但它适用于我们所需要的。
你想引用表的行吗? 在这种情况下,您应该使用DataTable.Rows属性。
试试这个:
notes.AddRange(table.Rows);
如果表行实现INote,那么这应该工作。
或者你可以像上面那样做:
foreach (INote note in table.Rows) { notes.Add(note) }
如果列表的属性与数据表中的字段名称匹配,则应该能够创建某种通用reflection例程。