我是LINQ的新手吗?请帮我 您如何将以下通用Lambda函数转换为lambda表达式:
objects.Where(objects=>values.Contains(objects.DistrictId) );
我正在尝试创建一个完整的lambda表达式而不需要任何或直接调用。类似的东西:
var innerItem = Expression.Parameter(typeof(Objects), "objects");
var innerProperty = Expression.Property(innerItem, "ID");
var innerMethodExpression = Expression.Call(innerProperty,null);
var innerLambda = Expression.Lambda<Func<Objects, bool>>(innerMethodExpression, innerItem);
var outerItem = Expression.Parameter(typeof(int[]), "item");
var containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(int) });
var containsMethodExpression = Expression.Call(innerMethodExpression, containsMethod, innerLambda);
var outerLambda = Expression.Lambda<Func<Objects, bool>>(containsMethodExpression, outerItem);
collection = collection.AsQueryable<Objects>().Where(outerLambda);
但我无法理解什么是错的
答案 0 :(得分:0)
在您的示例中,
Lambda函数是
objects.Where(objects=>values.Contains(objects.DistrictId) );
所以你的lambda表达式应该是
Expression<Func<Objects, int>> ObjectsExp = objects=>values.Contains(objects.DistrictId);
假设您的out结果是DistrictId的整数类型。