从 Linq-to-SQL 调用存储过程时强制转换无效
Linq-to-SQL 业务层调用 SQL 存储过程并获取:
Unable to cast object of type 'WhereSelectEnumerableIterator`2
[ERICustomersDataLayer.MergeCustomersResult,ERICustomersDataLayer.MergeCustomersResult]'
to type 'System.Collections.Generic.List`1[System.String]'.
存储过程:
ALTER PROCEDURE MergeCustomers @CurrentCustomerId UNIQUEIDENTIFIER, @MergedCustomerId UNIQUEIDENTIFIER
AS
BEGIN
DECLARE @Error VARCHAR(500);
IF NOT EXISTS (SELECT * FROM Customer WHERE Id = @CurrentCustomerId)
BEGIN
SET @Error = 'Current customer ID not in customer table '
SELECT @Error [Error]
RETURN -1
END
IF NOT EXISTS (SELECT * FROM Customer WHERE Id = @MergedCustomerId)
BEGIN
SET @Error = 'Merged customer ID not in customer table '
SELECT @Error [Error]
RETURN -1
END
END
Linq-to-SQL 代码:
public List<string> Merge(Guid CurrentUserId, Guid MergedUserId)
{
var dc = new ERICustomersDataLayer.ERICustomersDataContext();
var rows = (
from e in dc.MergeCustomers(CurrentUserId, MergedUserId)
select e);
return (List<string>)rows;
}
在测试用例中,SP 没有返回任何内容,因此这应该与空查询相同。但是,即使它确实返回错误消息,我也会得到相同的异常。
Linq-to-SQL business layer calls SQL stored procedure and gets:
Unable to cast object of type 'WhereSelectEnumerableIterator`2
[ERICustomersDataLayer.MergeCustomersResult,ERICustomersDataLayer.MergeCustomersResult]'
to type 'System.Collections.Generic.List`1[System.String]'.
Stored procedure:
ALTER PROCEDURE MergeCustomers @CurrentCustomerId UNIQUEIDENTIFIER, @MergedCustomerId UNIQUEIDENTIFIER
AS
BEGIN
DECLARE @Error VARCHAR(500);
IF NOT EXISTS (SELECT * FROM Customer WHERE Id = @CurrentCustomerId)
BEGIN
SET @Error = 'Current customer ID not in customer table '
SELECT @Error [Error]
RETURN -1
END
IF NOT EXISTS (SELECT * FROM Customer WHERE Id = @MergedCustomerId)
BEGIN
SET @Error = 'Merged customer ID not in customer table '
SELECT @Error [Error]
RETURN -1
END
END
Linq-to-SQL Code:
public List<string> Merge(Guid CurrentUserId, Guid MergedUserId)
{
var dc = new ERICustomersDataLayer.ERICustomersDataContext();
var rows = (
from e in dc.MergeCustomers(CurrentUserId, MergedUserId)
select e);
return (List<string>)rows;
}
In the test case, the SP is not returning anything, so this should be the same as an empty query. However, I get the same exception even when it does return an error message.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 driis 所说,linq 不返回列表,而是返回 MergeCustomersResult 的 IEnumerable。因此,您不能只是将其转换为列表或简单地执行 .ToList()。您需要确保您的 var“rows” 是一个字符串列表,然后才能将其作为一个返回。尝试这样的事情:
我不确定您的 MergeCustomerResult 是如何设置的,但我猜测它有一个名为 Error 的属性,该属性映射到您在 sp.return 中返回的 [Error] 列。
As driis said, the linq is not returning a list but an IEnumerable of MergeCustomersResult. So you can't just cast it into a List or simply do .ToList(). You need to make sure that your var "rows" is a list of string before you can return it as one. Try something like this:
I'm not sure how your MergeCustomerResult is setup but I'm guessing it has a property called Error that maps to the [Error] column you are returning in your sp.
您的错误只是说它无法将结果转换为列表。这是有道理的,因为 LINQ 查询返回的不是 List,而是
IQueryable
(或IEnumerable
),其实现基于实际表达式和底层提供程序。您可以轻松修复此错误。尝试使用
ToList
扩展方法将其转换为列表:Your error simply says it cannot cast the result to a List. This makes sense, because the LINQ query is not returning a List, but an
IQueryable
(orIEnumerable
), with an implementation based on the actual expression and underlying provider.You can fix this error easily. Try converting it to a list instead, using the
ToList
extension method: