将LINQ结果集转换为数据表时出现问题 [英] Problem in converting LINQ result set to datatable
本文介绍了将LINQ结果集转换为数据表时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好朋友
我必须将linq结果集转换为datatable
为此,我正在使用代码
Hello friends
i have to convert my linq result set to datatable
for this i am using the code
char HeadType = _session.HeadType;
var res = db.DispHed(_session.CompId;
DataView dv = _global.ToDataTable(db, res).DefaultView;
和类文件中的ToDataTable方法是
and ToDataTable methos in class file is
public DataTable ToDataTable(System.Data.Linq.DataContext ctx, object query)
{
DataTable dt = new DataTable();
if (query != null)
{
IDbCommand cmd = ctx.GetCommand(query as IQueryable);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = (SqlCommand)cmd;
try
{
cmd.Connection.Open();
adapter.FillSchema(dt, SchemaType.Source);
adapter.Fill(dt);
}
finally
{
cmd.Connection.Close();
}
}
return dt;
}
但在到达线
IDbCommand cmd = ctx.GetCommand(可查询为IQueryable);
它给出了错误
but on reaching line
IDbCommand cmd = ctx.GetCommand(query as IQueryable);
it is giving error
<pre lang="msil">Value cannot be null.<br />
Parameter name: query</pre>
在此先感谢
thanks in advance
推荐答案
问题与调用ToDataTable的方法有关:作为"query"传递的对象不为null,但未实现IQueryable接口.
结果,强制转换query as IQueryable
返回null,并且ctx.GetCommand
抱怨.
The problem is to do with whatever is calling ToDataTable: the object passed as "query" is not null, but it does not implement the IQueryable interface.
As a result, the castquery as IQueryable
returns null, andctx.GetCommand
complains.
这篇关于将LINQ结果集转换为数据表时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文