El código para este tipo de consultas sería algo así (y este es de los simples):
public List<string> GetSomeInfo(string fieldsToSearch, string contentTypesToSearch) { ... var queryval = string.Empty; if (contentTypesToSearch.IsNullOrEmpty()) queryval = string.Format("<Where>" + GenerateFieldsQuery(fieldsToSearch.Split(','), 0) + "</Where>", text); else queryval = string.Format("<Where><And>" + GenerateCTypesQuery(contentTypesToSearch.Split(','), 0) + GenerateFieldsQuery(fieldsToSearch.Split(','), 0) + "</And></Where>", text); var scope = "Scope=\"RecursiveAll\""; ... } private static string GenerateFieldsQuery(string[] fields, int index) { if (fields.Length == 0) return string.Empty; if (fields.Length == index + 1) return "<Contains><FieldRef Name='" + fields[index] + "' /><Value Type='Text'>{0}</Value></Contains>"; return "<Or><Contains><FieldRef Name='" + fields[index] + "' /><Value Type='Text'>{0}</Value></Contains>" + GenerateFieldsQuery(fields, ++index) + "</Or>"; } private static string GenerateCTypesQuery(string[] cTypes, int index) { if (cTypes.Length == 0) return string.Empty; if (cTypes.Length == index + 1) return "<Eq><FieldRef Name='ContentType' /><Value Type='Choice'>" + cTypes[index] + "</Value></Eq>"; return "<Or><Eq><FieldRef Name='ContentType' /><Value Type='Choice'>" + cTypes[index] + "</Value></Eq>" + GenerateCTypesQuery(cTypes, ++index) + "</Or>"; }
Eso era hasta ahora... gracias a Camlex (y gracias a Luis por decírmelo), este código se puede escribir así:
public List<string> GetSomeInfo(string fieldsToSearch, string contentTypesToSearch) { ... var queryVal = string.Empty; var fieldExtensions = new List<Expression<Func<SPListItem, bool>>>(); var cTypeExtensions = new List<Expression<Func<SPListItem, bool>>>(); if (!contentTypesToSearch.IsNullOrEmpty()) { foreach (var cType in contentTypesToSearch.Split(',')) cTypeExtensions.Add(x => (string)x["ContentType"] == cType); } foreach (var field in fieldsToSearch.Split(',')) fieldExtensions.Add(x => ((string)x[field]).Contains(text)); var expressions = new List<Expression<Func<SPListItem, bool>>>(); expressions.Add(ExpressionsHelper.CombineOr(cTypeExtensions)); expressions.Add(ExpressionsHelper.CombineOr(fieldExtensions)); queryVal = Camlex.Query().WhereAll(expressions).ToString(); ... }
Echaré de menos los métodos recursivos... me hacían sentir especial...
No hay comentarios:
Publicar un comentario