• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 116
  • Last Modified:

LINQ lambda expression to select from <tablename> where tablename is a variable chosen at runtime?

var query =
     (from c in [b]db.productInfo[/b]
     where [b]c.flavor[/b] == "Classic Coke" && [b]c.container[/b] == "Can"
     select new { c.co2Target }).Single();

Open in new window

Can the parts in bold be decided at runtime?

In other words, I want to use a context.<Table> and use LINQ lambda expression to do a select on the table, but I don't know which table until runtime.

Can I pass in the name of the table as a string into a method, and then inside the method use a lambda expression to do a select on <table name passed in> ?  Build the lambda expression dynamically?
Tom Knowlton
Tom Knowlton
1 Solution
Fernando SotoRetiredCommented:
The data source such as, db.productInfo, can not be dynamically selected. Filtering such as in the where and orderby clauses can be dynamically selected at run time but you will need to add a utility such as Castle.DynamicLinqQueryBuilder which can be installed into your project through NuGet. If you go to their website [HERE] you can download their project which also has a demo project which shows how to use it.
Tom KnowltonWeb developerAuthor Commented:
Thank you!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now