TrialUser
asked on
consuming the results of Linq query, convert to datatable
I have a LINQ query that selects records from the table.
1) I am wondering how can I convert this to datatable so I can bind this to a datagrid.
2) should I even be converting this to a datatable, is there a better way for working with the record set?
i am working in asp.net 4.0 application in vb.net
Any suggestions would be great. thanks
1) I am wondering how can I convert this to datatable so I can bind this to a datagrid.
2) should I even be converting this to a datatable, is there a better way for working with the record set?
i am working in asp.net 4.0 application in vb.net
Any suggestions would be great. thanks
Here is an example i use in my proj, for how to Linq-query a datatable n bind it to grdview(works for record set as well)
DataTable dtTemp = new DataTable();
dtTemp = (DataTable)ViewState["table"];
DataTable dtNewTable = new DataTable();
dtNewTable = dtTemp.Copy();
IEnumerable<DataRow> ienNewDataRow = default(IEnumerable<DataRow>);
ienNewDataRow = dtTemp.AsEnumerable()
.Where(t => t.Field<string>("CategoryNM") == ddlCategory.Text);
if (ienNewDataRow.Count() > 0)
dtNewTable = ienNewDataRow.CopyToDataTable();
else
dtNewTable = dtTemp.Clone();
gvResults.DataSource = dtNewTable;
gvResults.DataBind();
for converting linq result into a datatable,use this function..
eg.
var drevlinq = from drelinq_obj in context.drev where userid == drelinq_obj.user_master.us er_id select new { drelinq_obj.dreid };
DataTable devtab = LINQToDataTable(drevlinq);
eg.
var drevlinq = from drelinq_obj in context.drev where userid == drelinq_obj.user_master.us
DataTable devtab = LINQToDataTable(drevlinq);
public DataTable LINQToDataTable<T>(System.Collections.Generic.IEnumerable<T> varlist)
{
DataTable dtReturn = new DataTable();
// column names
PropertyInfo[] oProps = null;
if (varlist == null) return dtReturn;
foreach (T rec in varlist)
{
// Use reflection to get property names, to create table, Only first time, others
// will follow
if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
== typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow();
foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
(rec, null);
}
dtReturn.Rows.Add(dr);
}
return dtReturn;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Vivek:
Ur code works great to convert to datatable. does what I wanted. Thanks a ton for posting the vb version.
But I was wondering if it is also possible to convert it to a typed dataset. I am opening that as a separate question tough.
If it is possible, please respond. Thanks a ton.
Ur code works great to convert to datatable. does what I wanted. Thanks a ton for posting the vb version.
But I was wondering if it is also possible to convert it to a typed dataset. I am opening that as a separate question tough.
If it is possible, please respond. Thanks a ton.
myGridView.DataSource = from item in myList where ....