难以理解实体框架& Lambda表达式

时间:2009-06-24 14:07:10

标签: 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

我的声明我正在传递给我的存储库类

var qry = repositoryClass.Find(c => c.Categories.Where(d => d.CategoryParentID == typeID));

存储库类查找方法

        public IEnumerable<SomeModels> Find(Func<SomeModels, bool> exp)
    {
        return (from col in _db.SomeModels where exp select col);
    }

2 个答案:

答案 0 :(得分:4)

要使用EF,您需要Expression<...>Where应用(作为谓词):

public IEnumerable<SomeModels> Find(Expression<Func<SomeModels, bool>> exp)
{
    return _db.SomeModels.Where(exp);
}

然后你将其称为:

var qry = repositoryClass.Find(c => c.CategoryParentID == typeID);

然后将lambda翻译成Expression<...>

如果您的设置更复杂,请澄清。

答案 1 :(得分:0)

我刚在我的Repository Class中添加了一个方法

    public IEnumerable<Models> GetByCategory(int categoryID)
    {
        var qry = _db.ModelCategories.Where(p => p.CategoryID == categoryID).First();
        qry.Models.Load();

        return qry.Models;
    }

我猜是因为需要加载这是最好的方法。

相关问题