Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
DataTable dtRegistration = new DataTable();

oEntities BusinessObjects = new 0Entities();

protected void Page_Load(object sender, EventArgs e)
{
   var getcountries = BusinessObjects.GetCountry();

}

This is my code , I am using Entity Framework. I want getcountries to be populated in a DataTable.
Posted
Updated 23-Sep-13 3:34am
v3

N your collection is in the type of IEnumerable. So you can Convert it in to Datatable by using the below method
C#
public static DataTable ToDataTable<t>(this List<t> items)
{
    var tb = new DataTable(typeof(T).Name);

    PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

    foreach(var prop in props)
    {
        tb.Columns.Add(prop.Name, prop.PropertyType);
    }

     foreach (var item in items)
    {
       var values = new object[props.Length];
        for (var i=0; i<props.length;>        {
            values[i] = props[i].GetValue(item, null);
        }

        tb.Rows.Add(values);
    }

    return tb;
}

Hope this helps
 
Share this answer
 
v2
DataTable dtRegistration = new DataTable();

oEntities BusinessObjects = new 0Entities();

protected void Page_Load(object sender, EventArgs e)
{
var getcountries = BusinessObjects.GetCountry();
C#
foreach (var item in RegionCount)
{
                    count = item.region;
}



}
 
Share this answer
 
DataTable dtRegistration = new DataTable();

oEntities BusinessObjects = new 0Entities();

protected void Page_Load(object sender, EventArgs e)
{
var getcountries = BusinessObjects.GetCountry();
Collapse | Copy Code
foreach (var item in RegionCount)
{
count = item.region;
}


}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900