You don't need to pass count
as a parameter as you can get it from queries
Inside the method RecursiveNestedOr
itself (also, rename parameter query
as queries
).
Otherwise, your recursion looks fine to me. Here's how I'd "simplified" it, using LINQ (add using System.Linq
):
public static string RecursiveNestedOr(IEnumerable<string> queries)
{
if (queries == null || queries.Count() == 0)
return "";
if (queries.Count() == 1)
return queries.First();
// Now queries.Count is at least 2
return CAMLQueryBuilder.QueryOr(queries.Last(), RecursiveNestedOr(queries.Take(queries.Count() - 1)));
}
EDIT:
HowHere's how to do it without recursion (code not tested):
public static string NestedOr(IEnumerable<string> queries)
{
if (queries == null || queries.Count() == 0)
return "";
if (queries.Count() == 1)
return queries.First();
string result = queries.First();
// Now queries.Count is at least 2
foreach(string query in queries.Skip(1))
{
result = QueryOr(result, query);
}
return result;
}