从LINQ查询结果集中填充数据集或数据表 [英] Filling a DataSet or a DataTable from a LINQ query result set
问题描述
如何将LINQ查询公开为ASMX Web服务?
通常,从业务层,我可以返回类型化的 DataSet
或 DataTable
,可以对其进行序列化以通过ASMX进行传输.
How do you expose a LINQ query as an ASMX web service?
Usually, from the business tier, I can return a typed DataSet
or a DataTable
which can be serialized for transport over ASMX.
如何对LINQ查询执行相同的操作?
有没有一种方法可以通过LINQ查询填充类型化的 DataSet
或 DataTable
?
How can I do the same for a LINQ query?
Is there a way to populate a typed DataSet
or a DataTable
via a LINQ query?
public static MyDataTable CallMySproc()
{
string conn = "...";
MyDatabaseDataContext db = new MyDatabaseDataContext(conn);
MyDataTable dt = new MyDataTable();
// execute a sproc via LINQ
var query = from dr
in db.MySproc().AsEnumerable
select dr;
// copy LINQ query resultset into a DataTable -this does not work !
dt = query.CopyToDataTable();
return dt;
}
如何将LINQ查询的结果集放入 DataSet
或 DataTable
中?
另外,LINQ查询是否可以序列化,以便我可以将其公开为ASMX Web服务?
How could I put the result set of a LINQ query into a DataSet
or a DataTable
?
Alternatively, can the LINQ query be serializable so that I can expose it as an ASMX web service?
推荐答案
如问题中所述, IEnumerable
具有 CopyToDataTable
方法:
As mentioned in the question, IEnumerable
has a CopyToDataTable
method:
IEnumerable<DataRow> query =
from order in orders.AsEnumerable()
where order.Field<DateTime>("OrderDate") > new DateTime(2001, 8, 1)
select order;
// Create a table from the query.
DataTable boundTable = query.CopyToDataTable<DataRow>();
为什么这样对您不起作用?
Why won't that work for you?
这篇关于从LINQ查询结果集中填充数据集或数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!