How do you expose a LINQ query as an ASMX web service? Usually, from the business tier, I can return a typed DataSet
or DataTable
which can be serialized for transport over ASMX.
如何将LINQ查询公开为ASMX Web服务?通常,从业务层,我可以返回一个类型化的DataSet或DataTable,可以序列化以通过ASMX进行传输。
How can I do the same for a LINQ query? Is there a way to populate a typed DataSet
or DataTable
via a LINQ query?
如何为LINQ查询执行相同操作?有没有办法通过LINQ查询填充类型化的DataSet或DataTable?
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;
}
How can I get the result set of a LINQ query into a DataSet
or DataTable
? Alternatively, is the LINQ query serializeable so that I can expose it as an ASMX web service?
如何将LINQ查询的结果集合并到DataSet或DataTable中?或者,LINQ查询是否可序列化,以便我可以将其作为ASMX Web服务公开?
6 个解决方案
#1
78
As mentioned in the question, IEnumerable
has a CopyToDataTable
method:
正如问题中提到的,IEnumerable有一个CopyToDataTable方法:
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?
为什么那不适合你呢?
#2
24
To perform this query against a DataContext
class, you'll need to do the following:
要对DataContext类执行此查询,您需要执行以下操作:
MyDataContext db = new MyDataContext();
IEnumerable<DataRow> query =
(from order in db.Orders.AsEnumerable()
select new
{
order.Property,
order.Property2
})
as IEnumerable<DataRow>;
return query.CopyToDataTable<DataRow>();
Without the as IEnumerable<DataRow>;
you will see the following compilation error:
没有as IEnumerable
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)
无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'System.Collections.Generic.IEnumerable'。存在显式转换(您是否错过了演员?)
#3
21
Make a set of Data Transfer Objects, a couple of mappers, and return that via the .asmx.
You should never expose the database objects directly, as a change in the procedure schema will propagate to the web service consumer without you noticing it.
制作一组数据传输对象,几个映射器,然后通过.asmx返回。您永远不应该直接公开数据库对象,因为过程模式中的更改将传播到Web服务使用者,而不会注意到它。
#4
15
If you use a return type of IEnumerable
, you can return your query variable directly.
如果使用IEnumerable的返回类型,则可以直接返回查询变量。
#5
10
Create a class object and return a list(T)
of the query.
创建一个类对象并返回查询的列表(T)。
#6
1
If you use the return type of IEnumerable
.It helps return your query variable directly.
如果您使用IEnumerable的返回类型。它有助于直接返回您的查询变量。
MyDataContext db = new MyDataContext();
IEnumerable<DataRow> query =
(from order in db.Orders.AsEnumerable()
select new
{
order.Property,
order.Property2
})
as IEnumerable<DataRow>;
return query.CopyToDataTable<DataRow>();
#1
78
As mentioned in the question, IEnumerable
has a CopyToDataTable
method:
正如问题中提到的,IEnumerable有一个CopyToDataTable方法:
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?
为什么那不适合你呢?
#2
24
To perform this query against a DataContext
class, you'll need to do the following:
要对DataContext类执行此查询,您需要执行以下操作:
MyDataContext db = new MyDataContext();
IEnumerable<DataRow> query =
(from order in db.Orders.AsEnumerable()
select new
{
order.Property,
order.Property2
})
as IEnumerable<DataRow>;
return query.CopyToDataTable<DataRow>();
Without the as IEnumerable<DataRow>;
you will see the following compilation error:
没有as IEnumerable
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)
无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'System.Collections.Generic.IEnumerable'。存在显式转换(您是否错过了演员?)
#3
21
Make a set of Data Transfer Objects, a couple of mappers, and return that via the .asmx.
You should never expose the database objects directly, as a change in the procedure schema will propagate to the web service consumer without you noticing it.
制作一组数据传输对象,几个映射器,然后通过.asmx返回。您永远不应该直接公开数据库对象,因为过程模式中的更改将传播到Web服务使用者,而不会注意到它。
#4
15
If you use a return type of IEnumerable
, you can return your query variable directly.
如果使用IEnumerable的返回类型,则可以直接返回查询变量。
#5
10
Create a class object and return a list(T)
of the query.
创建一个类对象并返回查询的列表(T)。
#6
1
If you use the return type of IEnumerable
.It helps return your query variable directly.
如果您使用IEnumerable的返回类型。它有助于直接返回您的查询变量。
MyDataContext db = new MyDataContext();
IEnumerable<DataRow> query =
(from order in db.Orders.AsEnumerable()
select new
{
order.Property,
order.Property2
})
as IEnumerable<DataRow>;
return query.CopyToDataTable<DataRow>();