Cho cấu trúc lớp cha/con đơn giản. Tôi muốn sử dụng linqkit để áp dụng một biểu thức lambda con trên phụ huynh. Tôi cũng muốn biểu thức Lambda được cung cấp bởi một phương thức tiện ích.Hệ thống LinqKit.InvalidCastException Khi gọi biểu thức do phương thức cung cấp trên thuộc tính thành viên
public class Foo
{
public Bar Bar { get; set; }
}
public class Bar
{
public string Value { get; set; }
public static Expression<Func<Bar, bool>> GetLambdaX()
{
return c => c.Value == "A";
}
}
...
Expression<Func<Foo, bool>> lx = c => Bar.GetLambdaX().Invoke(c.Bar);
Console.WriteLine(lx.Expand());
Đoạn mã trên ném
System.InvalidCastException: Unable to cast object of type
'System.Linq.Expressions.MethodCallExpression' to type
'System.Linq.Expressions.LambdaExpression'.
at LinqKit.ExpressionExpander.VisitMethodCall(MethodCallExpression m)
at LinqKit.ExpressionVisitor.Visit(Expression exp)
at LinqKit.ExpressionVisitor.VisitLambda(LambdaExpression lambda)
at LinqKit.ExpressionVisitor.Visit(Expression exp)
at LinqKit.Extensions.Expand<TDelegate>(Expression`1 expr)
Tôi sẽ trợ giúp nhưng tôi không thể lấy mã của bạn để biên dịch. Dòng lx không hoạt động. – Nix
Không biên dịch cho tôi, dòng lx phải ở ví dụ như phương thức util – user293499