SingleResult.ToList 或 .Count 抛出 InvalidCastException
我有一个存储过程 GetTopRecords()。 sp 返回前 500 条记录。
- 我将 sp 从服务器资源管理器拖到 LinqtoSql 设计器界面。
- 我将返回类型更改为 Record
在代码中我有:
var x = myDataContext.GetTopRecords(); var y = x.Count(); var z = x.ToList();
上面最后两行之一抛出 InvalidCastException
,
x
的类型是
System.Data.Linq.SqlClient.SqlProvider.SingleResult<Record>
为什么我会收到异常?我做错了什么?请帮忙。
I have a stored procedure GetTopRecords(). The sp returns the top 500 records.
- I dragged the sp from the server explorer to the LinqtoSql designer surface.
- I changed the return type to Record
In code I have:
var x = myDataContext.GetTopRecords(); var y = x.Count(); var z = x.ToList();
either one of the last two lines above throws an InvalidCastException
the type of x
is
System.Data.Linq.SqlClient.SqlProvider.SingleResult<Record>
Why am I getting an exception? What am I doing wrong? Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
x 不是列表,它是单个元素。作为单个元素,您无法将其转换为包含多个元素的列表或计算它有多少个元素。
正如您所说“我将返回类型更改为记录”。你为什么这么做?
x is not a list, it is single element. As a single element you can't convert it to a list of more than one element or count how many elements it has.
As you say "I changed the return type to Record". Why did you do this?
由于这是 L2S (Linq To SQL),我假设它使用延迟加载,这意味着只有在调用立即执行的
Count
或ToList
时,执行查询。也就是说,问题应该与您从
GetTopRecords
方法返回的内容有关,该方法与Record
类型不兼容。您应该使用 L2S 设计器来检查该方法的结果是否正确映射到
Record
数据类型。这意味着Record
应该是 L2S 映射表,并且 GetTopRecords 应从该表返回数据。Since this is L2S (Linq To SQL), I'm assuming it's using Lazy loading, which means that only when calling
Count
orToList
, which are of immediate execution, the query is performed.That said, the problem should be related to what you are returning from the
GetTopRecords
method, which isn't compatible with theRecord
type.You should use the L2S designer to check if the result of that method is correctly mapped to a
Record
data type. Meaning thatRecord
should be a L2S mapped table and GetTopRecords should return data from that table.好吧,您能详细说明 GetTopRecords() 的内容吗?它返回什么?您需要统计热门记录吗?
var x = i.ToList();
var y = x.Count();
那么它将被允许,但是当您使用 tolist() 时,ToList 会迭代查询并创建 List<> 的实例使用查询的所有结果进行初始化。
count()也是一样,因为count也需要所有记录来进行计数。
tolist 的目的是用结果预填充数据。
Well,can you give details what GetTopRecords() .What it returns?do you need count of top records?
var x = i.ToList();
var y = x.Count();
then it will allowed,but when ever you use tolist() ToList iterates on the query and creates an instance of List<> initialized with all the results of the query.
which is same for count(),becuse count also need all records to perform counting.
the purpose of tolist is to prefilled data with result.
我认为自从生成 linq to sql 类以来,数据库模式发生了变化。因此,数据库表和表示它的 linq to Sql 对象之间存在不匹配。因此,调用失败,因为 .NET 无法成功从数据库创建的对象转换为 linq to sql 对象
I think there there have been changes to the database schema since the linq to sql classes were generated. therefore there is a mismatch between the database table and the linq to Sql object representing it. therefore, the calls fail because the .NET cannot successfully cast from the db created object to the linq to sql object
我不确定我是如何解决这个问题的。但现在已经修复了。如果有人遇到这个问题,请在此处添加注释,我将查找我的代码并发布正确的答案。虽然我感谢上面的回复者,但不幸的是,上面的答案都没有帮助。
I am not sure how I resolved this. But it is fixed now. If someone runs into this problem, please add a note here and I will look up my code and post the proper answer. While I thank the responders above, unfortunately, none of the answers above helped.