将 Linq 查询转换为 DataSet 失败
我正在将旧的 .net 应用程序迁移到 .net 4,此迁移必须分几个阶段完成,这就是为什么某些方法可能看起来有点不传统的原因。无论如何...
我所拥有的是一个存储过程(Analysis_select),返回一行和几列的结果。如果我在一切正常的情况下调用它
var result = dbContext.Analysis_select(user.UserId, Year, Week);
,我可以使用调试器查看数据或将其显示在网格视图或类似的东西中,因此表达式和存储过程确实有效!但结果与代码的其余部分不兼容,所以...
如果我尝试将其转换为 DataSet 它会失败,Visual Studio 实际上说这是可以的,但在网页上渲染时它崩溃了
var result = (DataSet)dbContext.Analysis_select(user.UserId, Year, Week);
错误如下
无法将“SingleResult`1[Analysis_select]”类型的对象转换为“System.Data.DataSet”类型。
我读过一些从 linq 到 DataSet 的其他转换,但大多数方法对此似乎有点过多。我想保留DataSet的原因是,取决于这样的结果有数万行代码。确实很糟糕,但是你能帮我解决这个问题吗?
非常感谢任何帮助,谢谢!
I'm migrating an older .net application to .net 4, this migration has to be done in several stages, thats why some of the methods might seem a bit unconventional. Anyway...
What I have is a Stored Procedure (Analysis_select) returning one row with several columns with the result. If i call it with
var result = dbContext.Analysis_select(user.UserId, Year, Week);
everything is fine, i can view the data in with the debugger or display it in a grid view or something like that, so the expression and Stored Procedure really works! But the result is not compatible with the rest of the code so...
If I try to cast it to DataSet it fails, Visual Studio actually sais this is ok but when rendering on a web page it crashes
var result = (DataSet)dbContext.Analysis_select(user.UserId, Year, Week);
The error is as follows
Unable to cast object of type 'SingleResult`1[Analysis_select]' to type 'System.Data.DataSet'.
I've read about some other conversions from linq to DataSet but most of the methods seems a bit excessive for this. The reason why I want to keep the DataSet is that there's tens of thousands of lines of code depending on such results. Sucks yes, but can you help me fix this?
Any help is highly appreciated, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我并不是建议这是一个很好的解决方案或最佳实践;肯定有一种不同的(而且可能更好)的方法。
如果您有 IEnumerable 并且没有其他方法来创建数据表,则可以使用反射。
你可以使用像下面这样的东西......
I'm not suggesting this as a great solution or best practices; there is most definitely a different (and probably better) way.
For a case where you have IEnumerable and no other means to create a data table, reflection can step in.
You could use something like below...
您需要编写一个扩展方法来将 IEnumerable 转换为 DataSet。下面是如何将 IEnumerable 转换为 DataTable 的示例。
以下是该解决方案来源的链接:http://www.chinhdo。 com/20090402/convert-list-to-datatable/
You will need to write an extension method to convert the IEnumerable into a DataSet. Here is an example of how to convert IEnumerable to a DataTable.
Here's a link to the source of this solution: http://www.chinhdo.com/20090402/convert-list-to-datatable/
您检查过本教程吗 http://msdn.microsoft.com/en-us/来自微软的library/bb386921.aspx?否则,LINQ 结果和数据集之间不会直接转换。
did you check this tutorial http://msdn.microsoft.com/en-us/library/bb386921.aspx from MS? Otherwise there is no direct conversion between LINQ result and Dataset.
使用 LINQ2SQL 存储过程,您永远不会获得数据集。您得到的正是 SingleResult。是一个 IEnumerable。
With LINQ2SQL stored procedures you never get DataSets. What you get is Exactly that, a SingleResult. Is an IEnumerable.