Entity framework 在实体框架和;表达式
我知道目前编译器不喜欢这个语句。获取错误Entity framework 在实体框架和;表达式,entity-framework,lambda,Entity Framework,Lambda,我知道目前编译器不喜欢这个语句。获取错误 Cannot convert lambda expression to delegate type 'System.Func<MyData.Models.SomeModels,bool>' because some of the return types in the block are not implicitly convertible to the delegate return type 存储库类查找方法 publi
Cannot convert lambda expression to delegate type 'System.Func<MyData.Models.SomeModels,bool>' because some of the return types in the block are not implicitly convertible to the delegate return type
存储库类查找方法
public IEnumerable<SomeModels> Find(Func<SomeModels, bool> exp)
{
return (from col in _db.SomeModels where exp select col);
}
public IEnumerable Find(Func exp)
{
返回(来自exp选择col的_db.SomeModels中的col);
}
要使用EF,您需要一个表达式
,该表达式(作为谓词)应用于,其中
:
public IEnumerable<SomeModels> Find(Expression<Func<SomeModels, bool>> exp)
{
return _db.SomeModels.Where(exp);
}
然后将lambda转换为表达式
如果您的设置更复杂,请澄清。要使用EF,您需要一个
表达式
,该表达式(作为谓词)应用于,其中
:
public IEnumerable<SomeModels> Find(Expression<Func<SomeModels, bool>> exp)
{
return _db.SomeModels.Where(exp);
}
然后将lambda转换为表达式
如果您的设置更复杂,请澄清。我刚刚在我的存储库类中添加了一个方法
var qry = repositoryClass.Find(c => c.Categories.Where(d => d.CategoryParentID == typeID));
public IEnumerable<Models> GetByCategory(int categoryID)
{
var qry = _db.ModelCategories.Where(p => p.CategoryID == categoryID).First();
qry.Models.Load();
return qry.Models;
}
public IEnumerable GetByCategory(int categoryID)
{
var qry=_db.modelcegories.Where(p=>p.CategoryID==CategoryID.First();
qry.Models.Load();
返回qry.模型;
}
我猜是因为它需要加载,这是最好的方法。我刚刚在我的Repository类中添加了一个方法
var qry = repositoryClass.Find(c => c.Categories.Where(d => d.CategoryParentID == typeID));
public IEnumerable<Models> GetByCategory(int categoryID)
{
var qry = _db.ModelCategories.Where(p => p.CategoryID == categoryID).First();
qry.Models.Load();
return qry.Models;
}
public IEnumerable GetByCategory(int categoryID)
{
var qry=_db.modelcegories.Where(p=>p.CategoryID==CategoryID.First();
qry.Models.Load();
返回qry.模型;
}
我猜这是因为它需要加载这是最好的方式。我不确定这是否是一种可以接受的方式,因此请分享任何更好的做法。我还不是Lambda最棒的。请看评论-我不是100%确定模型是什么样子的,所以很难理解。。。但是听起来你可能想要c=>c.Categories.Any(d=>…)我不确定这是否是一种可以接受的方法,所以请分享任何更好的实践。我还不是Lambda最棒的。请看评论-我不是100%确定模型是什么样子的,所以很难理解。。。但是听起来您可能想要c=>c.Categories.Any(d=>…)我仍然对repositoryClass.Find(c=>c.Categories.Where(d=>d.CategoryParentID==typeID))有问题;不知道你是否注意到,我正在尝试按类别获取模型。我有categoryid,但正在尝试查看Models.Categories是否包含它。这是我尝试在category属性上执行where的方法。Categories属性是一个列表,因为一个模型被列在多个类别中。我仍然对repositoryClass.Find(c=>c.Categories.where(d=>d.CategorityParentId==typeID))有问题;不知道你是否注意到,我正在尝试按类别获取模型。我有categoryid,但正在尝试查看Models.Categories是否包含它。这是我尝试在category propertyBTW上执行的一个方法,Categories属性是一个列表,因为模型在多个类别中列出。