How to create a Linq To Entities expression

HI, I'm using Linq To Entities and I'd like to convert this

return db.Products
         .Where(p => p.idUser.Equals(id) && 
                     p.Category.Genre.Any(g => g.visible))

into something like

Func<Genre, bool> expr = g => g.visible

return db.Products
         .Where(p => p.idUser.Equals(id) && 
                     p.Category.Genre.Any(expr))

so I can add more complexity with something like this

Func<Genre, bool> expr = g => g.visible
expr += g => g.position < 5

But I always have an 'internal 1025 error .NET'. Can anyone help me, please? Thanks.

Answers


You need to use Expressions, not delegates. You can use the PredicateBuilder class by Joseph Albahari to build your predicate dynamically :

Expression<Func<Genre, bool>> expr = g => g.visible;
expr = expr.And(g => g.position < 5);

Need Your Help

How to make a program that finds the number of happy numbers between 1 and 1 million

c++ numbers

I am trying to write code that calculates the right number of happy numbers between 1 and 1 million. What ends up being the result, however, is that either my output window remains blank and keeps on

Magento Installation stuck at configuration

database magento phpmyadmin ubuntu-9.10

I am not sure what is going wrong when I am trying to install magento to my local ubuntu machine.