0
votes

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?

1
Selecting from different tables will yield different result types. However it's possible using dynamic variables and chaining query using ifs - whose cases are selected by your comboboxes.Aurimas Neverauskas

1 Answers

1
votes

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.