如何使用Linq Query填充数据表中的数据 [英] how to fill data in datatable using Linq Query
本文介绍了如何使用Linq Query填充数据表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用此查询通过LINQ
从数据库中获取一些数据
i fetch some data from data base through LINQ
using this query
var data1 = from n in sqlclasses.Client_Infos
where n.CLIENT_ID.ToString().Equals(client_id)
select n;
现在data1拥有使用此命令使用gridveiw的所有数据
now data1 has all data it is working with gridveiw using this command
DetailsGrid.DataSource = data1;
DetailsGrid.DataBind();
但我想将这些数据添加到DataTable
如何帮助我...帮助我...
Thanx问候。
Ankush Sharma
but i want add this data into DataTable
How its Possible..Help me...
Thanx With Regards.
Ankush Sharma
推荐答案
使用System.Reflection添加此名称空间
Add this name space
using System.Reflection;
和方法是:
and method is:
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;
}
</t></t>
并拨打该方法
and call that method
DataTable dt = LINQToDataTable(data1);
试试这个工作......
Try this its working......
这篇关于如何使用Linq Query填充数据表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文