Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在动态LINQ中动态传递表和列_C#_Linq - Fatal编程技术网

C# 在动态LINQ中动态传递表和列

C# 在动态LINQ中动态传递表和列,c#,linq,C#,Linq,我找了很多,但找不到我要找的东西。这可能很简单。通常在LINQ中,我们编写如下选择查询: 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-c

我找了很多,但找不到我要找的东西。这可能很简单。通常在LINQ中,我们编写如下选择查询:

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
如何使用动态LINQ编写相同的查询?在这里,表名和列名将从一些winForm控件(textbox/combobox)或字符串中选择。我试过这个:

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

这是行不通的。谁能给我指一下正确的方向吗?

你不能那样做。您需要生成动态SQL并在数据库上运行它


有,但它没有你想要的那么动态

从不同的表中选择将产生不同的结果类型。然而,使用动态变量和使用ifs链接查询是可能的,ifs的案例由您的组合框选择。