tags:

views:

154

answers:

0

Hi,

I am trying to call Max, but am getting and error in CreateQuery saying that expression does not implement IQueryable<int>

What am I doing wrong here.

DataContext dc = new DataContext(Connection.ConnectionString);
typeof(Catalog).AssemblyQualifiedName.Dump();
Dictionary<string, IQueryable> dict = new Dictionary<string, IQueryable>() {
 {"Catalog", /*dc.GetTable(typeof(Catalog))*/Catalogs}
};

IQueryable expr = dict["Catalog"];
ParameterExpression start = Expression.Parameter(typeof(Catalog), "Catalog");

Expression left = Expression.PropertyOrField(start, "Name");
Expression right = Expression.Constant("Person");
Expression eq = Expression.Equal(left, right);

MethodCallExpression mceWhere = Expression.Call(
 typeof(Queryable),
 "Where",
 new System.Type[] { expr.ElementType },
 expr.Expression, 
 Expression.Lambda(eq, new ParameterExpression[] { start }));

ParameterExpression sel = Expression.Parameter(typeof(Catalog), "_");
var vsel = Expression.PropertyOrField(sel, "Version");
MethodCallExpression maxVersion = Expression.Call(
 typeof(Queryable),
 "Max",
 new System.Type[] { start.Type, vsel.Type },
 mceWhere,
 Expression.Lambda(vsel, new ParameterExpression[] { vsel.Expression as ParameterExpression }));

var results = expr.Provider.CreateQuery(maxVersion);