表达式不适用于 EF4.1
有人可以解释为什么以下不起作用吗?: 模型是这样定义的(非常简单的版本)
public class Monument
{
[Key]
public int? MonumentId { get; set; }
public string Name { get; set; }
}
我的存储库实现中的 Find 方法是这样定义的:
public virtual IQueryable<T> Find(Expression<Func<T, bool>> where)
{
return _dbset.Where(where);
}
where 参数以这种方式获取值:
whereClause = c => ( (FilteringRecord.MonumentId <= 0 ?
true : c.MonumentId == FilteringRecord.MonumentId)
&& (String.IsNullOrEmpty(FilteringRecord.Name) ?
true :c.Name.Contains(FilteringRecord.Name)) );
因此,如果 FilteringRecord.MonumentId 的值等于一个数字,我会得到所需的记录,但是如果 FilteringRecord.Name 有一个值,它将被完全忽略!为什么???
在实体框架或 LINQ to SQL 的第一个版本上同样可以正常工作!
提前致谢!!
Is there someone who can please explain why the following is not working??:
the model is defined like this (very simple version)
public class Monument
{
[Key]
public int? MonumentId { get; set; }
public string Name { get; set; }
}
The Find method in my repository implementation is defined like this:
public virtual IQueryable<T> Find(Expression<Func<T, bool>> where)
{
return _dbset.Where(where);
}
The where parameter gets a value this way:
whereClause = c => ( (FilteringRecord.MonumentId <= 0 ?
true : c.MonumentId == FilteringRecord.MonumentId)
&& (String.IsNullOrEmpty(FilteringRecord.Name) ?
true :c.Name.Contains(FilteringRecord.Name)) );
So if the value of FilteringRecord.MonumentId is equal to a number i get the desired record, but if the FilteringRecord.Name has a value it's completely ignored!! Why???
The same works fine on the first version of Entity Framework or LINQ to SQL!!
Thanks in advance!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,该表达式按预期工作!该错误是由 UI 中的错误绑定引起的(这是一个 WPF 应用程序),为 FilteringRecord 提供了错误的值!
Turns out that the expression is working as expected! The error was caused by a wrong binding in the UI (this is a WPF application), giving wrong values to FilteringRecord!