11using System ;
2+ using System . Collections . Generic ;
23using System . Data ;
34using System . Linq ;
45using NUnit . Framework ;
@@ -16,10 +17,10 @@ public void Setup()
1617 using ( var db = OpenDbConnection ( ) )
1718 {
1819 db . DropAndCreateTable < TestType > ( ) ;
19- db . Insert ( new TestType { Id = 1 , BoolCol = true , DateCol = new DateTime ( 2012 , 1 , 1 ) , TextCol = "asdf" , EnumCol = TestEnum . Val0 } ) ;
20- db . Insert ( new TestType { Id = 2 , BoolCol = true , DateCol = new DateTime ( 2012 , 2 , 1 ) , TextCol = "asdf123" , EnumCol = TestEnum . Val1 } ) ;
21- db . Insert ( new TestType { Id = 3 , BoolCol = false , DateCol = new DateTime ( 2012 , 3 , 1 ) , TextCol = "qwer" , EnumCol = TestEnum . Val2 } ) ;
22- db . Insert ( new TestType { Id = 4 , BoolCol = false , DateCol = new DateTime ( 2012 , 4 , 1 ) , TextCol = "qwer123" , EnumCol = TestEnum . Val3 } ) ;
20+ db . Insert ( new TestType { Id = 1 , BoolCol = true , DateCol = new DateTime ( 2012 , 1 , 1 ) , TextCol = "asdf" , EnumCol = TestEnum . Val0 , NullableIntCol = 10 } ) ;
21+ db . Insert ( new TestType { Id = 2 , BoolCol = true , DateCol = new DateTime ( 2012 , 2 , 1 ) , TextCol = "asdf123" , EnumCol = TestEnum . Val1 , NullableIntCol = null } ) ;
22+ db . Insert ( new TestType { Id = 3 , BoolCol = false , DateCol = new DateTime ( 2012 , 3 , 1 ) , TextCol = "qwer" , EnumCol = TestEnum . Val2 , NullableIntCol = 30 } ) ;
23+ db . Insert ( new TestType { Id = 4 , BoolCol = false , DateCol = new DateTime ( 2012 , 4 , 1 ) , TextCol = "qwer123" , EnumCol = TestEnum . Val3 , NullableIntCol = 40 } ) ;
2324 }
2425 Db = OpenDbConnection ( ) ;
2526 }
@@ -145,6 +146,69 @@ public void Can_Select_using_IN_using_object_array()
145146 Assert . AreEqual ( 3 , target . Count ) ;
146147 }
147148
149+ [ Test ]
150+ public void Can_Select_using_int_array_Contains ( )
151+ {
152+ var ids = new [ ] { 1 , 2 } ;
153+ var q = Db . From < TestType > ( ) . Where ( x => ids . Contains ( x . Id ) ) ;
154+ var target = Db . Select ( q ) ;
155+ CollectionAssert . AreEquivalent ( ids , target . Select ( t => t . Id ) . ToArray ( ) ) ;
156+ }
157+
158+ [ Test ]
159+ public void Can_Select_using_int_list_Contains ( )
160+ {
161+ var ids = new List < int > { 1 , 2 } ;
162+ var q = Db . From < TestType > ( ) . Where ( x => ids . Contains ( x . Id ) ) ;
163+ var target = Db . Select ( q ) ;
164+ CollectionAssert . AreEquivalent ( ids , target . Select ( t => t . Id ) . ToArray ( ) ) ;
165+ }
166+
167+ [ Test ]
168+ public void Can_Select_using_int_array_Contains_Value ( )
169+ {
170+ var ints = new [ ] { 10 , 40 } ;
171+ var q = Db . From < TestType > ( ) . Where ( x => ints . Contains ( x . NullableIntCol . Value ) ) ; // Doesn't compile without ".Value" here - "ints" is not nullable
172+ var target = Db . Select ( q ) ;
173+ CollectionAssert . AreEquivalent ( new [ ] { 1 , 4 } , target . Select ( t => t . Id ) . ToArray ( ) ) ;
174+ }
175+
176+ [ Test ]
177+ public void Can_Select_using_Nullable_HasValue ( )
178+ {
179+ var q = Db . From < TestType > ( ) . Where ( x => x . NullableIntCol . HasValue ) ; // WHERE NullableIntCol IS NOT NULL
180+ var target = Db . Select ( q ) ;
181+ CollectionAssert . AreEquivalent ( new [ ] { 1 , 3 , 4 } , target . Select ( t => t . Id ) . ToArray ( ) ) ;
182+
183+ q = Db . From < TestType > ( ) . Where ( x => ! x . NullableIntCol . HasValue ) ; // WHERE NOT (NullableIntCol IS NOT NULL)
184+ target = Db . Select ( q ) ;
185+ CollectionAssert . AreEquivalent ( new [ ] { 2 } , target . Select ( t => t . Id ) . ToArray ( ) ) ;
186+ }
187+
188+ [ Test ]
189+ public void Can_Select_using_constant_Yoda_condition ( )
190+ {
191+ var q = Db . From < TestType > ( ) . Where ( x => null != x . NullableIntCol ) ; // "null != x.NullableIntCol" should be the same as "x.NullableIntCol != null"
192+ var target = Db . Select ( q ) ;
193+ CollectionAssert . AreEquivalent ( new [ ] { 1 , 3 , 4 } , target . Select ( t => t . Id ) . ToArray ( ) ) ;
194+ }
195+
196+ [ Test ]
197+ public void Can_Select_using_int_array_constructed_inside_Contains ( )
198+ {
199+ var q = Db . From < TestType > ( ) . Where ( x => new int ? [ ] { 10 , 30 } . Contains ( x . NullableIntCol ) ) ;
200+ var target = Db . Select ( q ) ;
201+ CollectionAssert . AreEquivalent ( new [ ] { 1 , 3 } , target . Select ( t => t . Id ) . ToArray ( ) ) ;
202+ }
203+
204+ [ Test ]
205+ public void Can_Select_using_int_list_constructed_inside_Contains ( )
206+ {
207+ var q = Db . From < TestType > ( ) . Where ( x => new List < int ? > { 10 , 30 } . Contains ( x . NullableIntCol ) ) ;
208+ var target = Db . Select ( q ) ;
209+ CollectionAssert . AreEquivalent ( new [ ] { 1 , 3 } , target . Select ( t => t . Id ) . ToArray ( ) ) ;
210+ }
211+
148212 [ Test ]
149213 public void Can_Select_using_Startswith ( )
150214 {
@@ -240,5 +304,6 @@ public class TestType
240304 public DateTime DateCol { get ; set ; }
241305 public TestEnum EnumCol { get ; set ; }
242306 public TestType ComplexObjCol { get ; set ; }
307+ public int ? NullableIntCol { get ; set ; }
243308 }
244309}
0 commit comments