我正在尝试以更通用的方式编写以下简短的代码行。
我想要实现的是对自特定日期以来已更改或创建的记录进行简单计数。
public int GetChanges(DateTime LastActivityDate)
{
KHS.Innopas.Web.Library.DataModels.Documentation.DocumentationEntities ctx =
new KHS.Innopas.Web.Library.DataModels.Documentation.DocumentationEntities();
return ctx.Images.Count(row => row.CreatedAt > LastActivityDate);
}
我想将上面的示例更改为更通用的函数,例如
public int GetChanges(MetaTable table, string columnName, DateTime LastActivityDate)
{
return table.Count(row => row.columnName > LastActivityDate);
}
到目前为止我尝试的是
public int GetChanges(MetaTable table, string columnName, DateTime lastActivityDate)
{
// query = {Table(LanguageText)}
var query = table.GetQuery();
MetaColumn dateColumn;
table.TryGetColumn(columnName,out dateColumn);
if (dateColumn == null)
return new string[] {};
var entityParam = Expression.Parameter(table.EntityType, "row");
var property = Expression.Property(entityParam, dateColumn.Name);
var columnLambda = Expression.Lambda(property, entityParam);
var constant = Expression.Constant(lastActivityDate);
var GreaterThanCall = Expression.GreaterThan(columnLambda.Body, constant);
var whereLambda = Expression.Lambda(GreaterThanCall, entityParam);
var whereCall = Expression.Call(
typeof(Queryable),
"Count",
new Type[] { query.ElementType },
query.Expression,
whereLambda);
var result = query.Provider.CreateQuery(whereCall);
return result ;
}
现在的问题是,'whereCall' 不正确并引发(德语)错误。任何想法,它必须看起来如何?
谢谢你的帮助。塞巴斯蒂安
更多信息:这是德语错误:
Abfrageausdrücke für 'LINQ to Entities' können nur aus Instanzen konstruiert werden, die die die die IQueryable-Schnittstelle implementieren。参数名称:表达式