31991

How to create a Linq To Entities expression

Question:

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.

Answer1:

You need to use Expressions, not delegates. You can use the <a href="http://www.albahari.com/nutshell/predicatebuilder.aspx" rel="nofollow">PredicateBuilder</a> class by Joseph Albahari to build your predicate dynamically :

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

Recommend

  • Map for generic HList
  • Query Soundcloud API using created_at filter
  • xamarin ios 10 open app store application programmatically in my app
  • Is there a way of querying or accessing the structure of a Z3 expression via the API
  • dynamic cast not working for non polymorphic base class?
  • MySQL select genres issue (php)
  • SOLR - Querying Facets, return N results per Facet
  • Vim folding : how to hide all the single lines not containing a search pattern (or fold zero line)?
  • Scrolling News Ticker Jquery - Issues
  • Using extern @class in order to add a category?
  • “A GKScore must specify a leaderboard.”
  • Accessing another variable in the same class with a click event
  • Why can't UI components be accessed from a backgroundworker?
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • How to getText() from the input field of an angularjs Application
  • Build Successful but not running on simulator
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • Android fill_parent issue
  • FileReader+canvas image loading problem
  • Pass value from viewmodel to script in zk
  • Using $this when not in object context
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Deselecting radio buttons while keeping the View Model in synch
  • Getting last autonumber in access
  • Counter field in MS Access, how to generate?
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to check if every primary key value is being referenced as foreign key in another table
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • jquery mobile loadPage not working
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • KeystoneJS: Relationships in Admin UI not updating
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to stop GridView from loading again when I press back button?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal