C# 从 LINQ 查询结果集中填充 DataSet 或 DataTable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filling a DataSet or DataTable from a LINQ query result set
提问by Geoff Dalgas
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 查询执行相同的操作?有没有办法填充类型DataSet
或DataTable
通过 LINQ 查询?
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 serializable so that I can expose it as an ASMX web service?
如何将 LINQ 查询的结果集转换为 aDataSet
或DataTable
?或者,LINQ 查询是否可序列化,以便我可以将其公开为 ASMX Web 服务?
采纳答案by Jon Galloway
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?
为什么这对你不起作用?
回答by Dave Ward
If you use a return type of IEnumerable
, you can return your queryvariable directly.
如果您使用 的返回类型IEnumerable
,则可以直接返回您的查询变量。
回答by Brian Childress
Create a class object and return a list(T)
of the query.
创建一个类对象并返回一个list(T)
查询。
回答by Lars M?hlum
Make a set of Data Transfer Objects, a couple of mappers, and return that via the .asmx.
You should neverexpose the database objects directly, as a change in the procedure schema will propagate to the web service consumer without you noticing it.
制作一组数据传输对象,几个映射器,并通过 .asmx 返回。
您永远不应该直接公开数据库对象,因为过程架构中的更改将传播到 Web 服务使用者,而您不会注意到它。
回答by Lars M?hlum
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<DataRow>;
您将看到以下编译错误:
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”。存在显式转换(您是否缺少演员表?)
回答by Vijay S
If you use the return type of IEnumerable
.It helps return your query variable directly.
如果您使用IEnumerable
.It的返回类型,它有助于直接返回您的查询变量。
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>();
回答by Gabriel Magana
For the sake of completeness, these solutions do not work for EF Core (at least not for EF Core 2.2). Casting to IEnumerable<DataRow>
, as suggested in the other answers here, fails. Implementing this class and extension methods worked for me https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/implement-copytodatatable-where-type-not-a-datarow.
为完整起见,这些解决方案不适用于 EF Core(至少不适用于 EF Core 2.2)。铸造到IEnumerable<DataRow>
,如这里的其他答案建议,将失败。实现此类和扩展方法对我有用 https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/implement-copytodatatable-where-type-not-a-datarow。
Why it's not built into EF Core, I have not idea.
为什么它没有内置到 EF Core 中,我不知道。