Linq returing Object to DataTable
-
Monday, January 31, 2011 8:00 PMI have a function:public static Object getAllUsersAppsTimesForDate(int id, int dayID){var upit = (from tp in DMclass.db.TimesPeriodsselect new{tp.AppsTimesPeriods.Apps.Name,Start_Hour= tp.startHours ,});
return upit;}
and want to store it's results in DataTable.How to do that.
for example:DataTable x=getAllUsersAppsTimesForDate(1,1);//but here is error
All Replies
-
Sunday, April 03, 2011 12:08 AM
Here is a good link to do that http://blogs.msdn.com/b/erickt/archive/2007/08/24/linq-to-dataset-data-binding.aspx
Regards.
-
Wednesday, April 13, 2011 6:20 PM
public static DataTable getAllUsersAppsTimesForDate(int id, int dayID) { var upit = (from tp in DMclass.db.TimesPeriods select new { Name = tp.AppsTimesPeriods.Apps.Name, Start_Hour = tp.startHours , }); DataTable NewTable = new DataTable(); NewTable.Columns.Add(new DataColumn(typeof(String),"Name"); NewTable.Columns.Add(new DataColumn(typeof(Int32),"Hour"); upit.ToList().Foreach( (Element) => { DataRow NewRow = NewTable.NewRow(); NewRow.SetField<String>("Name") = Element.Name; NewRow.SetField<Int32>("Hour") = Element.Start_Hour; NewTable.Rows.Add(NewRow); }); return NewTable; }
Mark Post As Answer If It Helped You and Also Take Some Time Out To Mark The Thread Resolved.

