Pavel.S.
Pavel.S.

Reputation: 33

Expression failure

I've got such expression:

Linq2Rest.Reactive.InnerRestObservable`1[A]
    .Where(item => (Convert(IIF((item != null), item.ID, 0)) == Convert(61)))
    .Skip(0)
    .Take(20)

When I invoke Subscribe method on it I recieve such error:

variable 'item' of type 'A' referenced from scope '', but it is not defined

Can't figure out what is the problem. Actually can't see any problems with item argument...

UPD. Where clause built with this code:

public static IQbservable WhereExpression(this IQbservable query, Expression filterExpression, ParameterExpression instance = null)
        {
            if (instance == null)
                instance = Expression.Parameter(query.ElementType, "item"); // NOI18N

            var filteredQuery = (IQbservable)GenericsHelper.InvokeGenericExtensionMethod(
                typeof(Qbservable),
                "Where", // NOI18N
                new[] { query.ElementType },
                query,
                Expression.Lambda(filterExpression, instance)
            );

            return filteredQuery;
        }
public static object InvokeGenericExtensionMethod(
            Type extensionClass,
            string extensionMethodName,
            Type[] genericTypes,
            params object[] parameters
        )
        {
            var method = extensionClass.GetMethods().FirstOrDefault(m =>
                m.Name == extensionMethodName &&
                m.IsGenericMethod &&
                m.GetGenericArguments().Length == genericTypes.Length &&
                m.GetParameters().Length == parameters.Length
            );

            if (method == null)
                throw new ArgumentException(string.Format("Type {0} doesn't contain method {1}", extensionClass.Name, extensionMethodName)); // NOI18N

            var genericMethod = method.MakeGenericMethod(genericTypes);

            return genericMethod.Invoke(null, parameters);
        }

UPD 2. This is how WhereExpression calls:

foreach (var filter in filters)
            {
                var paramExpression = Expression.Parameter(query.ElementType, "item"); // NOI18N
                query = query.WhereExpression(filter.CreateFilterExpression(paramExpression), paramExpression);
            }

filters is collection of IFilterDescriptor interface from telerik.

Upvotes: 0

Views: 343

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174467

You need to use the same ParameterExpression instance both as the parameter and in the body of the expression.

The easiest thing would be to simply use the one from the filter expression, by using it completely.

Upvotes: 1

Related Questions