Fix InvalidCastException in DataTable.Select() with OR operator on SqlBoolean columns#127912
Open
pumpkin-bit wants to merge 1 commit intodotnet:mainfrom
Open
Fix InvalidCastException in DataTable.Select() with OR operator on SqlBoolean columns#127912pumpkin-bit wants to merge 1 commit intodotnet:mainfrom
pumpkin-bit wants to merge 1 commit intodotnet:mainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @SamMonoRT, @dotnet/efteam |
This was referenced May 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem/Bug
DataTable.Select()throwsInvalidCastExceptionwhen using theORoperator when columns are of typeSqlBoolean.In
BinaryNode.EvalBinaryOp, theOperators.Orbranch casts (bool)vLeft when the column is of typeSqlBooleanthe Eval() method returns a boxedSqlBoolean, not a bool. CLR unboxing requires exact type matching. Casting a boxed SqlBoolean to bool results in anInvalidCastException.The AND operator is implemented correctly nearby it has separate branches:
if (vLeft is bool) ... else { ((SqlBoolean)vLeft).IsFalse }. The OR branch was simply forgotten to be updated in the same way as AND.Solution
Replaced the unconditional
(bool)vLeftwith a type check with two branches, mirroring theANDlogicI've also attached a test that confirms my hypothesis. BEFORE my fixes, it throws the following crash:
AFTER my fixes: