使用Context - Entity Framework 4填充DataSet

Gib*_*boK 2 c# asp.net entity-framework

我有一些从Context返回的数据.数据已被拉动spCmsCategoriesReadHierarchy.

需要从Context中获取所有数据并填充我的DataSet.我的最终目标是使用DataSet对象填充TreeView控件.

有任何想法吗?感谢您的时间和耐心.

using (TestHierarchyEntities context = new TestHierarchyEntities())
{
    int n = 0;
    Int16 sl = 1;
    ObjectParameter nn = new ObjectParameter("nn", typeof(int));

    // Create a Data Set where adding the result of context 
    DataSet dataSet = new DataSet("myDataSet");

    foreach (CmsCategory categories in context.spCmsCategoriesReadHierarchy(n,sl,nn))
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

Bri*_*ins 5

当然,您可以手动将数据从对象复制到数据集中的数据行.

DataSet dataSet = new DataSet("myDataSet");
dataSet.Tables.Add(new DataTable());
//Setup the table columns.

foreach (CmsCategory categories in context.spCmsCategoriesReadHierarchy(n,sl,nn))
{
    DataRow row = dataSet.Tables[0].NewRow();
    row["A"] = categories.A;
    row["B"] = categories.B;

    dataSet.Tables[0].Rows.Add(row);
}
Run Code Online (Sandbox Code Playgroud)

如果您不希望将属性显式复制到数据行,则还可以使用反射进行复制.