| View previous topic :: View next topic |
| Author |
Message |
pavani
Joined: 26 Mar 2010 Posts: 37
|
Posted: Wed Jun 23, 2010 12:12 pm Post subject: How to Convert Linq provided data to data table? |
|
|
Hi Experts..,
I am using Linq on ADO.net Entity in my project.
I want retrieve the data and store it in data table. I know how to bind the data to data table using Sql. But using linq how it can be done. can any body help me please. _________________ Pavani.... |
|
| Back to top |
|
 |
rajesh
Joined: 26 Mar 2010 Posts: 43
|
Posted: Wed Jun 23, 2010 12:46 pm Post subject: |
|
|
Hello Pavani..,
There is no Predefined methods or classes like adapter to convert bind the linq resultant data to data table.
we need implement a method for it.
Here am giving you a sample method to convert Linq to data table.
public DataTable LINQtoDT<T>(IEnumerable<T> vlist)
{
DataTable dataReturn = new DataTable();
PropertyInfo[] Pro = null;
if (vlist == null) return dataReturn;
foreach (R rec in varlist)
{
if (Pro == null)
{
Pro = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pinfo in Pro)
{
Type colType = pinfo.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
== typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dataReturn.Columns.Add(new DataColumn(pinfo.Name, colType));
}
}
DataRow dr = dataReturn.NewRow();
foreach (PropertyInfo pinfo in Pro)
{
dr[pinfo.Name] = pinfo.GetValue(rec, null) == null ? DBNull.Value : pinfo.GetValue
(rec, null);
}
dataReturn.Rows.Add(dr);
}
return dataReturn;
} _________________
***************
Best Regards
Rajesh Mani.
Nyros Technologies. |
|
| Back to top |
|
 |
|