将实体模型转换为数据集 - 这可以完成吗?
这里非常沮丧... 我通常可以在网络上的某个地方找到 .Net 中复杂问题的某种答案,但我却找不到这个答案。 我所处的场景是,我必须将 LINQ to Entity 查询的结果转换为 DataSet,以便数据可以由现有业务逻辑进行处理,但我无法为此找到单一的工作解决方案。
我尝试过像 EntityCommand 生成读取器这样的基本方法,但是这个方法不起作用,因为 DataTable.Load() 会引发异常(EntityCommand 生成的读取器不支持 GetSchemaTable() )。
我还尝试了更多[据说]友好的方法,例如 Entity to IDataReader(http://l2edatareaderadapter.codeplex.com/),但是这个会抛出异常,文档很少,并且自 2008 年以来就没有被触及过。
另一种方法我发现在这里(http://blogs.msdn.com/b/alexj/archive/2007/11/27/Hydrating-an-entitydatareader-into-a-datatable-part-1.aspx),但没有工作副本代码;只是片段。
我很难相信,首先微软不会提供这种开箱即用的向后兼容项目,其次,它也不会是由社区创建的。
如果有可用的商业解决方案,我也愿意考虑。
谢谢!
Very frustrated here ...
I can usually find an answer of some kind to complex issues in .Net somewhere on the net, but this one eludes me.
I'm in a scenario where I have to convert the result of a LINQ to Entity query into a DataSet so the data can then be processed by existing business logic, and I can't find a single working solution out ther for this.
I've tried basic approaches like the EntityCommand generating a reader, but this one does not work because DataTable.Load() thorws an excpetion (the reader generated by EntityCommand does not support GetSchemaTable() ).
I've also tried more [supposedly] friendly approaches like Entity to IDataReader(http://l2edatareaderadapter.codeplex.com/), but this one throws exceptions, has very little docs, and hasn't been touched since 2008.
Another approach I found is here (http://blogs.msdn.com/b/alexj/archive/2007/11/27/hydrating-an-entitydatareader-into-a-datatable-part-1.aspx), but does not have a working copy of the code; only snippets.
I find it hard to believe that first of all MS would not have offered this backwards-compatibility item out of the box, and second, that it would not have been created by the community either.
I'm willing to look at commercial solutions as well if any are available.
Thx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将结果转换为列表,并使用以下命令将列表转换为数据表。
http://social.msdn.microsoft.com/Forums/br/csharpgeneral/thread/6ffcb247-77fb-40b4-bcba-08ba377ab9db
希望这对
Preetam有帮助
You can convert the result into a list and use the following to convert the list to a datatable.
http://social.msdn.microsoft.com/Forums/br/csharpgeneral/thread/6ffcb247-77fb-40b4-bcba-08ba377ab9db
Hope this helps
Preetam
这可能不是最好的解决方案,但如果您的场景只有一两个表需要添加到数据集中,为什么不直接手动构建它们。
}
ds.Tables.Add(tbl);
Col1 和 Col2 来自 Linq To Entity 对象,您可以像这样创建所需的所有表并返回 DataSet。
This might not be the greatest solution, but if your scenario have only one or two table that you need to add to the DataSet, why not build them directly manually.
}
ds.Tables.Add(tbl);
The Col1 and Col2 comes from your Linq To Entity objects, you can create all the table you need like this and return your DataSet.
这是一个灵活的代码,应该可以满足您的大部分需求:
This is a flexible code and should handle most of your needs: