- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有一种方法可以检查 SqlParameterCollection 中的 ReturnValue 参数,如果存在则检查该值是否违反约束。
public static void VerifyUniqueness(this ISqlServerProvider provider,
OperationType operationType, SqlParameterCollection cp)
在这个方法中,我认为首先检查就足够了:
if (cp["@ReturnValue"]==null){return;}
但是如果“@ReturnValue”== null 会抛出异常,所以我想我会使用 Linq:
cp.FirstOrDefault(p => p.ParameterName == "@ReturnValue");
但这甚至无法编译。
'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.Linq 被引用,所以不确定发生了什么。
最佳答案
您不能使用 FirstOrDefault
的原因是那个SqlParameterCollection
不执行 IEnumerable<SqlParameter>
(在其上定义了 LINQ 表达式)。
检查参数的预期方法是使用 Contains
if (cp.Contains("@ReturnValue"))
如果你真的想使用 LINQ 运算符,你可以使用 Cast
:
cp.Cast<SqlParameter>()
.FirstOrDefault(p => p.ParameterName == "@ReturnValue")
关于c# - 无法在 SqlParameterCollection 上使用 FirstOrDefault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17311018/
我遇到这样的问题:无论内部表达式如何计算,FirstOrDefault 始终返回它遇到的第一个项目。 假设我有一系列公司。 Companies = {Company1, Company2, Compa
我正在尝试在 IEnumerable 中搜索特定记录(使用谓词)。如果该记录不存在,我想返回第一条记录。如果那也不存在,我只想要 null。 我正在使用 var category = categori
有什么区别 FirstOrDefault(someField => someField.Name.Equals(settings.Text)) 和 Where(someField => someFie
使用 Linq to Entities 有以下区别吗? db.EntityName.Where(a => a.Id == id).FirstOrDefault(); db.EntityName.Fir
myCollection.Where(...).FirstOrDefault() 和 myCollection.FirstOrDefault(...) 之间是否存在性能差异 用您正在使用的谓词填充点。
我知道这个问题是asked a lot by people甚至有人说 So, first(FirstOrDefault(predicate)) one is better in terms of pe
有什么区别吗 DataFeedManager.LoadAllDataFeeds().FirstOrDefault(d => d.ItemID == itemId); 和 DataFeedManage
我一直在编写我的 LINQ 查询时使用 Where 子句中的谓词,后跟 FirstOrDefault 子句。我开始在 FirstOrDefault 子句中看到带有谓词的示例。 这个比那个好吗? EF
这两个 Linq 查询有什么区别: var result = ResultLists().Where( c=> c.code == "abc").FirstOrDefault(); // vs. va
我有点好奇,当谈到 FirstOrDefault 时,哪种被认为是“最佳实践”。 我已经看过这个问题,它与我的问题相似,但还不够接近,无法回答我的问题。 其中哪一个是“更好的代码”?为什么? var
FirstOrDefault() and FirstOrDefault() 有什么区别?在 LINQ 中? 有人能给我一些简单的解释吗? TypedDataSet ds= codeComponent.
我想声明如下: var block = blocksById.FirstOrDefault(X => X.Value == tracResult.ID).Key 我的问题是如果我有 FirstOrDe
我在 C# 项目中使用 EntityFramework 6.1.3 和 SQL Server。我有两个查询,基本上应该执行相同的操作。 1. Exams.GroupBy(x=>x.SubjectID)
这个问题在这里已经有了答案: EF Where(x => x.ColumnVal == 1) vs FirstOrDefault(x => x.Column == 1) (3 个答案) 关闭 9 年
我一直在清理一些旧代码,并遇到了以下结构的一些查询: var attribute = DbSet.Where().Select(a => a.Attribute).SingleOrDefault();
希望做一些重构......使用 NHibernate 我目前有这个查询 public Widget FindByCode(string code) { return
这里遇到了一种边缘情况问题。我的任务是将所有数据从一个数据库提取到另一个数据库,其中目标数据库具有不同的架构。 我选择编写一个 WinForms 实用程序来在必要时使用 Entity Framewor
下面是我编写的代码,由于 FirstOrDefault,它只返回第一条记录。获取存储在变量 EmailAddresses 中的所有电子邮件地址的语法应该是什么,其中 FlagActive 字段为 Tr
有两个主键a和b一键编译两把 key 怎么样 var product = db.TMP_HELLO.FirstOrDefault(instrmnt => (instrmnt.a == good.a;
当使用 Enumerable.FirstorDefault() 时,我是否需要始终捕获当操作的集合为 null 时可能抛出的 ArumentNullException? 过去我总是这样做: Workf
我是一名优秀的程序员,十分优秀!