How to create a Linq To Entities expression

HI, I use Linq To Entities, and I would 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 error 1025.NET. Can anybody help me? Thank.

0
source share
1 answer

You need to use Expressions, not delegates. You can use the PredicateBuilderJoseph Albahari class to dynamically create your predicate:

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

Source: https://habr.com/ru/post/1687367/


All Articles