How to simulate the SQL WHERE IN clause in a SPQuery
Simulate a WHERE IN clause in a SPQuery
If you ever have used something like
1
SELECT * FROM table WHERE field IN ('value1','value2','value3','value4')
in T-SQL, and wanted to mimic this functionality in the SharePoint SPQuery class, you probably have found out there is no IN statement in the CAML Query language. So what do do? Here’s a way to simulate WHERE IN clause in a SPQuery statement.
One solution is to use a LinQ query that looks something like this:
1
2
3
4
5
6
7
8
9
10
var values = new List {"value1", "value2", "value3"};
var listItems = from SPListItem tItem in list.Items
orderby tItem.Title ascending
where values.Contains(tItem["Title"].ToString())
select tItem;
foreach (var spListItem in listItems)
{
Console.WriteLine(spListItem.Title);
}
However, this has a serious performance problem. Because the .AllItems property will do a full T-SQL select all items behind the scenes, it can get very slow if you have thousands and thousand of items in your list. It do look elegant, however.
Another way is to build up a CAML query that does an OR statement for every item that you have in your list of values. Essentially, you will have to build up something that looks similar to this CAML query:
1
2
3
4
5
value1
value2
value3
Notice that the CAML syntax for multiple OR statements is a little bit odd. You would at first glance think that is would look like this:
1
2
3
4
5
value1
value2
value3
But this will throw an error. You will need to have nest the OR statements within each other.
For this purpose I have written a small helper method that will build the CAML query for a given list of values in a List of strings. You call it like this:
1
2
3
4
5
6
7
8
9
10
11
12
SPQuery query = new SPQuery();
query.RowLimit = 2000;
query.ViewFields = "";
var whereClause = GetWhereClause("Title", "Eq", new List
{
"value1", "value2", "value3"
});
query.Query = string.Format(@"{0}", whereClause);
// Get the list items
SPListItemCollection items = list.GetItems(query);
The first parameter is the field that you want to comapre on, the second parameter is the type (“Eq” makes most sense in this scenario), and the third parameter is of List of strings. Here’s the code for the helper method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
private static string GetWhereClause(string fieldName, string whereElement, IEnumerable list)
{
var sb = new StringBuilder();
var makeFirstEntry = true;
foreach (var entry in list)
{
var savedQuery = sb.ToString();
sb = new StringBuilder();
if (!makeFirstEntry)
{
sb.Append("");
sb.Append(savedQuery);
}
else
{
sb.Append(MakeStatement(whereElement, fieldName, entry));
makeFirstEntry = false;
continue;
}
sb.Append(MakeStatement(whereElement, fieldName, entry));
sb.Append("");
}
return sb.ToString();
}
private static string MakeStatement(string whereElement, string fieldName, string entry)
{
var sb = new StringBuilder();
sb.Append(string.Format("<{0}>", whereElement));
sb.Append(string.Format("{1}", fieldName, entry));
sb.Append(string.Format("", whereElement));
return sb.ToString();
}