Skip to content

Commit d1247f5

Browse files
authored
#160 Unhandled expression type: 'Index' (#162)
* #160 Unhandled expression type: 'Index' * #160 Unhandled expression type: 'Index'
1 parent e6d2954 commit d1247f5

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/LinqKit.Core/ExpressionVisitor.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public virtual Expression Visit(Expression exp)
8484
case ExpressionType.ListInit:
8585
return VisitListInit((ListInitExpression)exp);
8686
#if !NET35
87+
case ExpressionType.Index:
88+
return VisitIndex((IndexExpression)exp);
89+
8790
case ExpressionType.Extension:
8891
return VisitExtension(exp);
8992
#endif
@@ -400,6 +403,20 @@ protected virtual Expression VisitInvocation(InvocationExpression iv)
400403
Expression expr = Visit(iv.Expression);
401404
return args != iv.Arguments || expr != iv.Expression ? Expression.Invoke(expr, args) : iv;
402405
}
406+
407+
#if !NET35
408+
/// <summary> Visit index expression </summary>
409+
protected virtual Expression VisitIndex(IndexExpression exp)
410+
{
411+
var obj = Visit(exp.Object);
412+
var args = VisitExpressionList(exp.Arguments);
413+
if (obj != exp.Object || args != exp.Arguments)
414+
{
415+
return Expression.MakeIndex(obj, exp.Indexer, args);
416+
}
417+
return exp;
418+
}
419+
#endif
403420
}
404421
}
405422
#endif

tests/LinqKit.Tests.Net452/ExpressionExpanderTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Linq.Expressions;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
25
using Xunit;
36

47
namespace LinqKit.Tests.Net452
@@ -28,5 +31,20 @@ public void ExpressionCombiner_Expression_UnaryPlus()
2831
var executed = exp.Expand().ToString();
2932
Assert.Equal(exp.ToString(), executed);
3033
}
34+
35+
[Fact]
36+
public void ExpressionExpander_Expression_Index()
37+
{
38+
var listParameter = Expression.Parameter(typeof(List<string>), "l");
39+
Expression<Func<List<string>, string>> expression1 = Expression.Lambda<Func<List<string>, string>>(Expression.MakeIndex(
40+
listParameter,
41+
typeof(List<string>).GetProperties().SingleOrDefault(p => p.GetIndexParameters().Length > 0),
42+
new[] { Expression.Constant(0) }),
43+
listParameter);
44+
Expression<Func<string, string>> expression2 = s => s;
45+
46+
var executed = Linq.Expr((List<string> l) => expression2.Invoke(expression1.Invoke(l))).Expand().ToString();
47+
Assert.Equal(expression1.ToString(), executed);
48+
}
3149
}
3250
}

0 commit comments

Comments
 (0)