I construct a dynamic search linq expression.
I'm able to count the number of records in a List but if I change the List to a BindingList I can't use the property Count in my Lambda expression. I get the following error:
An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll
Additional information: The specified type member 'Count' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
Here is a little sample:
public class Toto
{
BindingList<Tata> tatas; // or List<Tata> tatas;
}
I make the query as following:
var c = System.Linq.Expressions.Expression.Parameter(typeof(Toto), c);
var member = System.Linq.Expressions.Expression.PropertyOrField(c, "tatas");
var memberCount = System.Linq.Expressions.Expression.PropertyOrField(member, "Count");
var constantValue = System.Linq.Expressions.Expression.Constant(2);
var countExpression = System.Linq.Expressions.Expression.Equal(memberCount, constantValue);
var lambdaExpression = System.Linq.Expressions.Expression.Lambda<Func<Bike, bool>>(countExpression, c);
using (var context = new Context())
{
var listResult = context.Totos.Where(lambdaExpression).ToList();
Console.WriteLine(listResult.Count);
}
If tatas is of type List this code works great but I can't figure out how I can use the Count property on BindingList to make my lambda expression work.