Paradox
Paradox

Reputation: 129

Pass the table and column dynamically in dynamic LINQ

I searched a lot, but couldn't find what I was looking exactly. This may be very simple. Normally in LINQ we write the select query like this:

var entityModel = new StudentEntities();
var dept = (from a in entityModel.STUDENT where a.NAME != null select a.DEPARTMENT).Distinct().OrderBy(w => w); 
//STUDENT- table, NAME-column name, DEPARTMENT-column name

How to write the same query using dynamic LINQ? Here the table name and column name names will be selected from either some winForm controls (textbox/combobox) or string. Tried this:

var dept = "(from a in entityModel." + tableName + "where a." + cbo1.Text.ToString + "!= null select a."+cbo2.Text.ToString +").Distinct().OrderBy(w => w)";

This is not working. Can anyone point me to the right direction, please?

Upvotes: 0

Views: 654

Answers (1)

İsmet Alkan
İsmet Alkan

Reputation: 5447

You can't do that. You need to generate your dynamic SQL and run it over your database.

There's this library: Dynamic LINQ, but it isn't as dynamic as you want.

Upvotes: 1

Related Questions