[英]Unable to use FirstOrDefault on SqlParameterCollection
There is a method that examines an SqlParameterCollection for a ReturnValue parameter and if present checks that value for a constraint violation. 有一种方法可以检查ReturnValue参数的SqlParameterCollection,如果存在,则检查该约束违规的值。
public static void VerifyUniqueness(this ISqlServerProvider provider,
OperationType operationType, SqlParameterCollection cp)
Within this method I thought it was enough to first check: 在这种方法中,我认为首先检查是足够的:
if (cp["@ReturnValue"]==null){return;}
but that throws an exception if "@ReturnValue" == null so then I thought I'd use Linq: 但是如果“@ReturnValue”== null则会引发异常,所以我认为我会使用Linq:
cp.FirstOrDefault(p => p.ParameterName == "@ReturnValue");
but that wont even compile. 但那甚至不会编译。
'System.Data.SqlClient.SqlParameterCollection' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Data.SqlClient.SqlParameterCollection' could be found (are you missing a using directive or an assembly reference?)
'System.Data.SqlClient.SqlParameterCollection'不包含'FirstOrDefault'的定义,并且没有可以找到接受类型'System.Data.SqlClient.SqlParameterCollection'的第一个参数的扩展方法'FirstOrDefault'(你是否缺少using指令?或汇编参考?)
System.Linq is referenced so not sure what's going on. System.Linq被引用,因此不确定发生了什么。
The reason you can't use FirstOrDefault
is that SqlParameterCollection
does not implement IEnumerable<SqlParameter>
(on which the LINQ expressions are defined). 你不能使用
FirstOrDefault
的原因是SqlParameterCollection
没有实现IEnumerable<SqlParameter>
(在其上定义了LINQ表达式)。
The intended method of checking for a parameter is to use Contains
检查参数的预期方法是使用
Contains
if (cp.Contains("@ReturnValue"))
If you really want to use LINQ operators, you can use Cast
: 如果你真的想使用LINQ运算符,你可以使用
Cast
:
cp.Cast<SqlParameter>()
.FirstOrDefault(p => p.ParameterName == "@ReturnValue")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.