By using LINQ to XML, you can produce succinct code. The following LINQ query transforms CamlWhere child activities into CAML XML.

    private CamlQuery BuildCamlQuery(List<string> viewFields)
    {
      CamlQuery camlQuery = new CamlQuery();

      camlQuery.ViewXml = new XElement(CamlElements.View,
        new XElement(CamlElements.Query,
          this.Activities.Cast<CamlWhere>()
          .Select(activity =>
            CamlElements.Where(
            activity.ComparisonOperator.ToString(),
            activity.FieldRef.Expression.ToString(),
            activity.ValueType.ToString(),
            (activity.Value.Expression as VisualBasicValue<Object>).ExpressionText.ToString()
            )),
        new XElement(CamlElements.ViewFields,
          viewFields
          .Select(name => CamlElements.ViewFieldNames(name))),
        new XElement(CamlElements.RowLimit, 100))).ToString();

      return camlQuery;
    }

Last edited Nov 8, 2011 at 4:02 AM by dvana, version 5

Comments

No comments yet.