从 LINQ 查询结果集中填充 DataSet 或 DataTable [英] 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 查询结果集中填充 DataSet 或 DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!