【发布时间】:2011-02-01 19:32:14
【问题描述】:
我已经定义了一个进行数据库交互的 GenericRepository 类。
protected GenericRepository rep = new GenericRepository();
在我的 BLL 课程中,我可以像这样查询数据库:
public List<Album> GetVisibleAlbums(int accessLevel)
{
return rep.Find<Album>(a => a.AccessLevel.BinaryAnd(accessLevel)).ToList();
}
BinaryAnd 是一种扩展方法,它逐位检查两个 int 值。例如AccessLevel=5 => AccessLevel.BinaryAnd(5) 和 AccessLevel.binaryAnd(1) 都返回 true。
但是我不能在我的 LINQ 查询中使用这种扩展方法。我收到如下运行时错误:LINQ to Entities does not recognize the method 'Boolean BinaryAnd(System.Object, System.Object)' method, and this method cannot be translated into a store expression.
还尝试将其更改为自定义方法,但没有运气。有哪些解决方法?
我是否应该获取所有专辑,然后通过 foreach 循环迭代它们并选择与 AccessLevels 匹配的那些?
【问题讨论】:
标签: linq c#-4.0 linq-to-entities extension-methods