Exemple #1
0
 protected override Expression VisitMemberInitExpression(MemberInitExpression expression)
 {
     foreach (var binding in expression.Bindings)
     {
         var camlElement = new CamlElement();
         camlElement.Name = binding.Member.Name;
         _queueElements.Push(camlElement);
     }
     return(expression);
 }
Exemple #2
0
        private void GetCamlType(Type type, CamlElement camlElement)
        {
            switch (Type.GetTypeCode(type))
            {
            case TypeCode.Int32:
                camlElement.Type = "Number";
                if (_lookupField)
                {
                    camlElement.Type = "Integer";
                    _lookupField     = false;
                }
                break;

            case TypeCode.String:
                camlElement.Type = "Text";
                if (_lookupField)
                {
                    camlElement.Type = "Choice";
                    _lookupField     = false;
                }
                break;

            case TypeCode.DateTime:
                camlElement.Type = "DateTime";
                break;

            case TypeCode.Double:
                camlElement.Type = "Number";
                break;

            case TypeCode.Boolean:
                camlElement.Type = "Bool";
                break;

            default:
                throw new NotImplementedException();
            }
        }
        public string GenerateQuery(Queue <CamlElement> lstOfElement)
        {
            var tempElements     = new Queue <CamlElement>();
            var arranjedElements = lstOfElement.Select(i => new { item = i, Path = i.Name + i.Operator + i.Type });
            var groupedIn        = arranjedElements.GroupBy(i => i.Path);

            foreach (var group in groupedIn)
            {
                if (group.Count() != 1)
                {
                    var inElement = new CamlElement();
                    inElement.Name     = group.First().item.Name;
                    inElement.Logical  = CamlLogical.And;
                    inElement.Operator = CamlOperators.In;
                    inElement.Values   = group.Select(i => i.item.Value).ToArray();
                    inElement.Type     = group.First().item.Type;
                    tempElements.Enqueue(inElement);
                }
                else
                {
                    var camlElement = new CamlElement();
                    camlElement.Name     = group.First().item.Name;
                    camlElement.Logical  = group.First().item.Logical;
                    camlElement.Operator = group.First().item.Operator;
                    camlElement.Value    = group.First().item.Value;
                    camlElement.Type     = group.First().item.Type;
                    tempElements.Enqueue(camlElement);
                }
            }
            lstOfElement = tempElements;
            var          queryJoin = new StringBuilder();
            const string query     = @"<{0}><FieldRef Name='{1}' {6}/><Value {2} Type='{3}'>{4}</Value></{5}>";

            if (lstOfElement.Count > 0)
            {
                var itemCount = 0;
                while (lstOfElement.Count != 0)
                {
                    var element = lstOfElement.Dequeue();
                    itemCount++;
                    var date     = string.Empty;
                    var lookupId = string.Empty;
                    // Display only Date
                    if (String.Compare(element.Type, "DateTime", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        date = "IncludeTimeValue='false'";
                    }
                    if (String.Compare(element.Type, "Integer", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        lookupId = "LookupId='TRUE'";
                    }


                    if (element.Operator == CamlOperators.In)
                    {
                        queryJoin.AppendFormat("<In><FieldRef Name='{0}' /><Values>", element.Name);
                        foreach (var val in element.Values)
                        {
                            queryJoin.AppendFormat("<Value Type='{0}'>{1}</Value>", element.Type, val);
                        }
                        queryJoin.AppendFormat("</Values></In>");
                    }
                    else
                    {
                        queryJoin.AppendFormat
                            (query, element.Operator, element.Name,
                            date, element.Type, element.Value, element.Operator, lookupId);
                    }


                    if (itemCount < 2)
                    {
                        continue;
                    }
                    queryJoin.Insert(0, string.Format("<{0}>", element.Logical));
                    queryJoin.Append(string.Format("</{0}>", element.Logical));
                }
                queryJoin.Insert(0, "");
                queryJoin.Append("");
            }
            return(queryJoin.ToString());
        }
Exemple #4
0
        private void GetLogical(Expression expression)
        {
            CamlElement camlElement = null;

            switch (expression.NodeType)
            {
            case ExpressionType.And:
            case ExpressionType.AndAlso:
                //camlElement.Logical = CamlLogical.And;
                break;

            case ExpressionType.Or:
            case ExpressionType.OrElse:
                //camlElement.Logical =  CamlLogical.Or;
                break;

            case ExpressionType.NotEqual:
                camlElement          = new CamlElement();
                camlElement.Operator = CamlOperators.Neq;
                _queueElements.Push(camlElement);
                break;

            case ExpressionType.Equal:
                camlElement          = new CamlElement();
                camlElement.Operator = CamlOperators.Eq;
                _queueElements.Push(camlElement);
                break;

            case ExpressionType.GreaterThanOrEqual:
                camlElement          = new CamlElement();
                camlElement.Operator = CamlOperators.Geq;
                _queueElements.Push(camlElement);
                break;

            case ExpressionType.GreaterThan:
                camlElement          = new CamlElement();
                camlElement.Operator = CamlOperators.Gt;
                _queueElements.Push(camlElement);
                break;

            case ExpressionType.LessThan:
                camlElement          = new CamlElement();
                camlElement.Operator = CamlOperators.Lt;
                _queueElements.Push(camlElement);
                break;

            case ExpressionType.LessThanOrEqual:
                camlElement          = new CamlElement();
                camlElement.Operator = CamlOperators.Leq;
                _queueElements.Push(camlElement);
                break;

            case ExpressionType.Call:
                var methodExpression = (MethodCallExpression)expression;
                camlElement = new CamlElement();
                if (methodExpression.Method.Name.Equals("Equals"))
                {
                    camlElement.Operator = CamlOperators.Eq;
                }
                if (methodExpression.Method.Name.Equals("StartsWith"))
                {
                    camlElement.Operator = CamlOperators.BeginsWith;
                }
                if (methodExpression.Method.Name.Equals("Contains"))
                {
                    camlElement.Operator = CamlOperators.Contains;
                }


                _queueElements.Push(camlElement);
                break;

            default:
                throw new NotImplementedException();
            }
        }