Friday, June 15, 2007

Using "AND" in SPQuery

Level: Beginner

Ok, here is a small tip for the beginners. If you are trying to search records based on two values, for example, "select * from list where a=1 and b=2" then how will you write its equivalent using the SPQuery object. Here is the sample:

SPQuery query = new SPQuery();
query.Query = "<Where><And><Eq><FieldRef Name='Field1'></FieldRef><Value Type='Number'>1</Value></Eq><Gt><FieldRef Name='Field2'></FieldRef><Value Type='Number'>0</Value></Gt></And></Where>";

This query will find records where Field1 = 1 and Field2 > 0.

So you have learnt two new things, using "And" and "Gt".

No comments:

Post a Comment