将Linq结果分配给Datatable [英] Assigning Linq result to Datatable
本文介绍了将Linq结果分配给Datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从linq查询的结果是 var(IEnumrable<'T'> anonymous type<'string,int>')
我希望结果在数据表或数据集中。
解决方案
我创建了一个名为public的方法LINQToDataTable如下:
public DataTable LINQToDataTable< T>(IEnumerable< T> varlist)
{
DataTable dtReturn = new DataTable();
//列名称
PropertyInfo [] oProps = null;
if(varlist == null)return dtReturn;
foreach(T rec in varlist)
{
//使用反射获取属性名称,创建表,只有第一次,其他
将跟随
if(oProps == null)
{
oProps =((Type)rec.GetType())GetProperties();
foreach(oProps中的PropertyInfo pi)
{
类型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(oProps中的PropertyInfo pi)
{
dr [pi.Name] = pi.GetValue(rec,null)== null?DBNull.Value:pi.GetValue
(rec,null);
}
dtReturn.Rows.Add(dr);
}
return dtReturn;
}
示例II
这是我的第二个方法:
public DataTable ToDataTable(System.Data.Linq.DataContext ctx ,object query)
{
if(query == null)
{
throw new ArgumentNullException(query);
}
IDbCommand cmd = ctx.GetCommand(query as IQueryable);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand =(SqlCommand)cmd;
DataTable dt = new DataTable(sd);
try
{
cmd.Connection.Open();
adapter.FillSchema(dt,SchemaType.Source);
adapter.Fill(dt);
}
finally
{
cmd.Connection.Close();
}
return dt;
}
I am getting the result from linq query as var(IEnumrable<'T'> anonymous type<'string,int>')
i want the result to be in the datatable or dataset.
解决方案
I found two sample for that issue;
Sample I:
I created a public method called LINQToDataTable as following:
public DataTable LINQToDataTable<T>(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;
}
Sample II
Here is my second method:
public DataTable ToDataTable(System.Data.Linq.DataContext ctx, object query)
{
if (query == null)
{
throw new ArgumentNullException("query");
}
IDbCommand cmd = ctx.GetCommand(query as IQueryable);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = (SqlCommand)cmd;
DataTable dt = new DataTable("sd");
try
{
cmd.Connection.Open();
adapter.FillSchema(dt, SchemaType.Source);
adapter.Fill(dt);
}
finally
{
cmd.Connection.Close();
}
return dt;
}
这篇关于将Linq结果分配给Datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文