Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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#MVC:Func<;表1,“;运行时类型"&燃气轮机;如何获取动态类型?_C#_Linq_Lambda_Sql Order By_Expression - Fatal编程技术网

C#MVC:Func<;表1,“;运行时类型"&燃气轮机;如何获取动态类型?

C#MVC:Func<;表1,“;运行时类型"&燃气轮机;如何获取动态类型?,c#,linq,lambda,sql-order-by,expression,C#,Linq,Lambda,Sql Order By,Expression,嘿,我正在尝试根据用户单击的内容对列的自定义数据网格进行排序。变量“sort”被传递给控制器,但它只是一个表示要排序的列的字符串 我需要获取LambdaExpression中要使用的列的类型。。。这是密码 ParameterExpression param = Expression.Parameter(typeof(Table1), "x"); MemberExpression memberExp = Expression.Property(param, sort); var lam

嘿,我正在尝试根据用户单击的内容对列的自定义数据网格进行排序。变量“sort”被传递给控制器,但它只是一个表示要排序的列的字符串

我需要获取LambdaExpression中要使用的列的类型。。。这是密码

ParameterExpression param = Expression.Parameter(typeof(Table1), "x");
MemberExpression memberExp = Expression.Property(param, sort);      
var lambdaExp = Expression.Lambda<Func<Table1, int>>(memberExp, new ParameterExpression[] { param });

if (bool.Parse(Session["sort"].ToString()))
    sortedKeys = keys.OrderBy(lambdaExp).Skip((currentPage - 1) * _pageSize).Take(_pageSize);
else
    sortedKeys = keys.OrderByDescending(lambdaExp).Skip((currentPage - 1) * _pageSize).Take(_pageSize);
ParameterExpression param=Expression.Parameter(typeof(表1),“x”);
MemberExpression memberExp=Expression.Property(参数,排序);
var lambdaExp=Expression.Lambda(memberExp,新参数Expression[]{param});
if(bool.Parse(会话[“sort”].ToString())
sortedKeys=keys.OrderBy(lambdaExp).跳过((当前页面-1)*\u页面大小).获取(\u页面大小);
其他的
sortedKeys=keys.OrderByDescending(lambdaExp)。跳过((当前页面-1)*\u页面大小)。获取(\u页面大小);
正如您在第#3行中看到的,我在该行中传递了一个类型为int的列的委托Func works from,但该值将根据单击的列而动态变化

我如何解决这个问题


谢谢

我假设您手头有列类型,它将是System.type。我们称之为“columnType”。因此,您要做的是使用非泛型Lambda方法:

Type funcType = typeof(Func<,>).MakeGenericType(typeof(Table1), columnType);
Expression.Lambda(funcType, memberExp, new ParameterExpression[] { param });
Type funcType=typeof(Func).MakeGenericType(typeof(表1),columnType);
Lambda(funcType,memberExp,新参数Expression[]{param});