DBLinq.Firebird中Single()与SingleOrDefault实现异常的技术咨询
SingleOrDefault Implementation I ran into a significant issue with the SingleOrDefault method in the DBLinq.Firebird/FirebirdSql.Data.FirebirdClient provider, and wanted to break down the problem clearly:
The Problem
When querying a table that has multiple records matching the filter (for example, multiple rows where CD = 5), executing this LINQ query:
dbContext.GetTable<TABLE>().SingleOrDefault(r => r.CD == 5)
does not throw an exception as it should.
Expected vs. Actual Behavior
- Expected: Per standard LINQ contract,
SingleOrDefaultshould throw anInvalidOperationExceptionwhen more than one matching record exists — this method is designed for scenarios where you expect 0 or exactly 1 result. - Actual: The method returns the first matching record without any error, completely ignoring the presence of additional matching rows.
Root Cause
Digging into the underlying SQL generated by the provider reveals the core issue:
SELECT FIRST 2 * FROM TABLE WHERE (CD == @CD)
The provider correctly fetches up to 2 records (a standard approach to detect multiple matches), but the SingleOrDefault implementation fails to validate if the result set contains 2 records. It simply returns the first record instead of triggering the expected exception.
内容的提问来源于stack exchange,提问作者MariusB

