0

I know currently the compiler is not liking this statement. Getting Error

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

My Statement I'm passing to my Repository Class

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

Repository Class Find Method

        public IEnumerable<SomeModels> Find(Func<SomeModels, bool> exp)
    {
        return (from col in _db.SomeModels where exp select col);
    }
2
  • I'm not sure if this is an acceptable way of doing this so please share any better practices. I'm not the greatest at Lambda yet. Commented Jun 24, 2009 at 14:09
  • Re the comment - I'm not 100% sure what the model looks like, so hard to follow... but it sounds like you might want c=>c.Categories.Any(d=>...) Commented Jun 24, 2009 at 17:37

2 Answers 2

4

To work with EF you need an Expression<...>, applied (as a predicate) with Where:

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

You'd then call that as:

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

The lambda is then translated into an Expression<...>.

If your setup is more complex, please clarify.

Sign up to request clarification or add additional context in comments.

2 Comments

I am still having issues with the repositoryClass.Find(c => c.Categories.Where(d => d.CategoryParentID == typeID)); Not sure if you notice by i'm trying to get models by category. I have categoryid but trying to see if Models.Categories contains it. This is way I was trying to do a where on the category property
BTW the Categories Property is a List<Categories> due to a model being listed in multiple categories.
0

I just added a method in my Repository Class

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

        return qry.Models;
    }

I'm guessing because it needs to be loaded this is the best way to go.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.