在try 执行linq查询时,我遇到以下错误:
LINQ to实体无法识别方法的布尔值
我读过很多以前的问题,人们都会遇到同样的错误,如果我理解正确,那是因为LINQ to Entities需要将整个LINQ查询表达式转换为服务器查询,因此不能在其中调用外部方法.我还没能把我的情景转换成有效的东西,我的大脑开始崩溃,所以我希望有人能给我指出正确的方向.我们正在使用实体框架和规范模式(我对两者都不熟悉).
以下是使用该规范的代码:
ISpecification<Charity> specification = new CharitySearchSpecification(charityTitle, charityReference);
charities = charitiesRepository.Find(specification).OrderBy(p => p.RegisteredName).ToList();
下面是Linq表达式:
public System.Linq.Expressions.Expression<Func<Charity, bool>> IsSatisfied()
{
return p => p.IsCharityMatching(this.charityName, this.charityReference);
}
下面是IsCharityMatching方法:
public bool IsCharityMatching(string name, string referenceNumber)
{
bool exists = true;
if (!String.IsNullOrEmpty(name))
{
if (!this.registeredName.ToLower().Contains(name.ToLower()) &&
!this.alias.ToLower().Contains(name.ToLower()) &&
!this.charityId.ToLower().Contains(name.ToLower()))
{
exists = false;
}
}
if (!String.IsNullOrEmpty(referenceNumber))
{
if (!this.charityReference.ToLower().Contains(referenceNumber.ToLower()))
{
exists = false;
}
}
return exists;
}
如果你需要更多信息,请告诉我.
非常感谢,
安妮莉