Count
метод, который вы ищете имеет только один параметр:
public static int Count<TSource>(this IEnumerable<TSource> source)
Вы также можете использовать Count
свойство, поскольку источником является ICollection
:
//build a lambda: (Bar bar) => bar.Data.Count > 1;
var barParam = Expression.Parameter(typeof (Bar), "bar");
var barDataProperty = Expression.PropertyOrField(barParam, "Data");
//since Data is of type ICollection, we can use the Count Property
var count = Expression.PropertyOrField(barDataProperty, "Count");
//if you do not want this dependency, call the Count() extension method:
var enumerableCountMethod = typeof (Enumerable).GetMethods()
.First(method => method.Name == "Count" && method.GetParameters().Length == 1)
.MakeGenericMethod(typeof(Foo));
var count2 = Expression.Call(enumerableCountMethod, barDataProperty);
var comparison = Expression.GreaterThan(count, Expression.Constant(1));
var comparison2 = Expression.GreaterThan(count2, Expression.Constant(1));
Expression<Func<Bar, bool>> expr = Expression.Lambda<Func<Bar, bool>>(comparison, barParam);
Expression<Func<Bar, bool>> expr2 = Expression.Lambda<Func<Bar, bool>>(comparison2, barParam);
var list = new List<Bar>();
var filteredList = list.Where(expr.Compile());
var filteredList2 = list.Where(expr2.Compile());
Зачем вам нужно создавать его динамически? Вся необходимая информация известна во время компиляции; просто используйте лямбду. – Servy
@Servy есть возможность создать его динамически –
@ JonahPereira вы можете уточнить? По какой причине вы не могли передать объект IEnumerable? –
Jacob