Exemple #1
0
        private int GetLookupIdByValue(string fieldName, object value)
        {
            SPField lookupField           = GetLookupField(fieldName);
            string  cacheKey              = String.Concat("LookupValue-", this.ListId, "-", fieldName);
            Dictionary <string, int> dict = this.ObjectCache[cacheKey] as Dictionary <string, int>;

            if (this.ObjectCache[cacheKey] == null)
            {
                this.ObjectCache[cacheKey] = dict = new Dictionary <string, int>();
            }

            ICamlParameterBinding binding = CamlParameterBinding.GetValueBinding(this.Site, lookupField, value);
            string lookupValue            = binding.Bind(CamlExpression.EmptyBindings);
            int    lookupId;

            if (dict.TryGetValue(lookupValue, out lookupId))
            {
                if (lookupId == 0)
                {
                    throw new ArgumentOutOfRangeException("value");
                }
                return(lookupId);
            }

            if (lookupField.ParentList.ItemCount == 0)
            {
                throw new ArgumentOutOfRangeException("value");
            }
            if (dict.Count == 0 && lookupField.ParentList.ItemCount < 1000)
            {
                foreach (SPListItem item in lookupField.ParentList.GetItems(lookupField.InternalName))
                {
                    ICamlParameterBinding b1 = CamlParameterBinding.GetValueBinding(this.Site, lookupField, item[lookupField.Id]);
                    dict[b1.Bind(CamlExpression.EmptyBindings)] = item.ID;
                }
                if (dict.TryGetValue(lookupValue, out lookupId))
                {
                    return(lookupId);
                }
                throw new ArgumentOutOfRangeException("value");
            }

            SPQuery query = new SPQuery();

            query.ViewFields = Caml.ViewFields(SPBuiltInFieldName.ID).ToString();
            query.Query      = Caml.Equals(lookupField.InternalName, binding).ToString();
            query.RowLimit   = 1;

            SPListItemCollection collection = lookupField.ParentList.GetItems(query);

            if (collection.Count > 0)
            {
                dict[lookupValue] = collection[0].ID;
                return(collection[0].ID);
            }
            dict[lookupValue] = 0;
            throw new ArgumentOutOfRangeException("value");
        }
Exemple #2
0
        private CamlExpression HandleEqualityComparison(SPModelQueryFieldInfo s, CamlBinaryOperator op)
        {
            ICamlParameterBinding value      = currentScope.GetValueBinding(s);
            CamlExpression        expression = new CamlWhereBinaryComparisonExpression(op, s.FieldRef, value);
            Type memberType = currentScope.MemberType;

            if (memberType.IsGenericType && memberType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                memberType = memberType.GetGenericArguments()[0];
            }
            if (memberType.IsValueType || memberType == typeof(string))
            {
                string defaultValue = value.Bind(new Hashtable {
                    { currentScope.ParameterName, memberType.IsValueType ? memberType.GetDefaultValue() : "" }
                });
                CamlExpression lateBoundCond = new CamlLateBoundDefaultValueAsNullExpression(s.FieldRef, value, defaultValue);
                return(op == CamlBinaryOperator.Eq ? expression | lateBoundCond : expression& ~lateBoundCond);
            }
            return(expression);
        }
Exemple #3
0
 /// <summary>
 /// Called when visiting a binary expression inside a &lt;Where/&gt; element.
 /// </summary>
 /// <param name="operatorValue">Type of binary operator.</param>
 /// <param name="fieldName">Name of field.</param>
 /// <param name="value">Value to operate against the field.</param>
 /// <param name="includeTimeValue">Indicates whether time component is included in comparison.</param>
 protected internal abstract void VisitWhereBinaryComparisonExpression(CamlBinaryOperator operatorValue, CamlParameterBindingFieldRef fieldName, ICamlParameterBinding value, bool?includeTimeValue);