0

I have the following predicate expression.

public IQueryable<T> Find(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
    {
        return //boolean
    }

Here is where I am calling the method:

    public void TestMethod1()
    {
        author.AuthorName = authorName;

        using (var context = new AspBlogRepository<Author>())
        {
            if (context.Find(e => e.AuthorName == authorName))
            {
                //do nothing
            }
            context.Add(author);   
        }
    }

I get an error saying that I you cannot convert an IQueryable to bool. I just want to be able to use my predicate expression to see if the author is already in the database.

Any help would be much appreciated.

Thanks!

3
  • 1
    This if is pointless, isn't it? if (context.Find(e => e.AuthorName == authorName)) { //do nothing } context.Add(author); Have you forgotten to add an else before context.Add? Feb 17, 2013 at 22:15
  • Not in this case because EF will throw an error if I try to insert an author who is already in the database. Feb 17, 2013 at 22:16
  • 1
    @LanceCollins in this case do nothing is also pointless. Inverse condition and remove that empty block Feb 17, 2013 at 22:21

3 Answers 3

1
public IQueryable<T> Find(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
    {
        return //boolean
    }

The problem is here, if you indeed return boolean. If this method is inside your context, you would first need to resolve Author and then filter the Authors in your repository.

If this is an EF context, and you have access to the EF context in your repository, you should be able to get the db set like this:

var entitySet = context.Set<T>();

Then, you can run your predicate against the entitySet returning the filtered results.

1

Error says it all. context.Find(e => e.AuthorName == authorName) is returning IQueryable<T>. If is expecting a bool

So your usage of if (context.Find(e => e.AuthorName == authorName)) is wrong. Change it to

if (context.Find(e => e.AuthorName == authorName).Any())
{

}
1

context.Find returns an IQueryable<T>. Your code could instead look like:

if (context.Find(e => e.AuthorName == authorName).Count() == 1)
{
    //do nothing
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.