error while call WhereAny Method

Jan 14, 2011 at 7:28 AM

 

I try to use this code:

var typesFilter = new[] { "Type1", "Type2" };
var expressions = new List<Expression<Func<SPListItem, bool>>>(4);
for (int i = 0; i < typesFilter.Length; i++)
     expressions.Add(x => (string)x["SomeType"] == typesFilter[i]);
camlexQuery = CamlexNET.Camlex.Query().WhereAny(expressions);

I have error "Index was outside the bounds of the array." in row 

 

camlexQuery = CamlexNET.Camlex.Query().WhereAny(expressions);

StackTrace:

at lambda_method(ExecutionScope )

at CamlexNET.Impl.Factories.OperandBuilder.evaluateExpression(Expression expr)

at CamlexNET.Impl.Factories.OperandBuilder.createValueOperandFromNonConstantExpression(Expression expr, Type explicitOperandType, Expression sourceExpr)

at CamlexNET.Impl.Factories.OperandBuilder.CreateValueOperandForNativeSyntax(Expression expr, Type explicitOperandType, Expression sourceExpr)

at CamlexNET.Impl.Factories.OperandBuilder.CreateValueOperandForNativeSyntax(Expression expr, Type explicitOperandType)

at CamlexNET.Impl.Factories.OperandBuilder.CreateValueOperandForNativeSyntax(Expression expr)

at CamlexNET.Impl.NullabilityBaseAnalyzer.IsValid(LambdaExpression expr)

at CamlexNET.Impl.Operations.IsNull.IsNullAnalyzer.IsValid(LambdaExpression expr)

at CamlexNET.Impl.Factories.AnalyzerFactory.isNullExpression(LambdaExpression expr, IsNullAnalyzer& analyzer)

at CamlexNET.Impl.Factories.AnalyzerFactory.Create(LambdaExpression expr)

at CamlexNET.Impl.CompositeExpressionBaseAnalyzer.isExpressionValid(Expression subExpr, ParameterExpression lambdaParam)

at CamlexNET.Impl.CompositeExpressionBaseAnalyzer.IsValid(LambdaExpression expr)

at CamlexNET.Impl.Operations.OrElse.OrElseAnalyzer.IsValid(LambdaExpression expr)

at CamlexNET.Impl.GenericTranslator.TranslateWhere(LambdaExpression expr)

at CamlexNET.Impl.Query.Where(Expression`1 expr)

at CamlexNET.Impl.Query.WhereAny(IEnumerable`1 expressions)

at ConsoleApplication1.Program.Main(String[] args) in c:\Users\igor\Documents\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 28

at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)

at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

at System.Threading.ThreadHelper.ThreadStart()

 

 

I need your help!

Developer
Jan 14, 2011 at 7:39 AM

You are using a closure which is linked to "i" variable.

The closure is executed AFTER "for" is finished - so "i" will be always equal to 2 - that's why "typesFilter[i]" (which converts to "typesFilter[2]" always) expression throws the exeption :-)

So it is not a problem of Camlex.NET - but just inappropriate usage of closures (btw, Resharper usually notices it)

So the solution is simple (changed part is underlined):

for (int i = 0; i < typesFilter.Length; i++)
{
var index = i;
expressions.Add(x => (string)x["SomeType"] == typesFilter[index]);
}
Jan 14, 2011 at 7:55 AM

vtimashkov, thank u! Resharper tell me about it too:)