Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/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# 如何获取Func的键<;Q、 字符串>;基于字符串键_C#_Linq_C# 4.0_Func_C# 6.0 - Fatal编程技术网

C# 如何获取Func的键<;Q、 字符串>;基于字符串键

C# 如何获取Func的键<;Q、 字符串>;基于字符串键,c#,linq,c#-4.0,func,c#-6.0,C#,Linq,C# 4.0,Func,C# 6.0,我试图编写一个通用方法来获取排序键以执行OrderBy操作,但在下面的代码中失败了 public Func<Q, string> GetSortProperty<Q>(IQueryable<Q> data, string SortColumn) { if (!string.IsNullOrWhiteSpace(sortColumn)) { Func<Q, string> sort = i => i.GetTy

我试图编写一个通用方法来获取排序键以执行
OrderBy
操作,但在下面的代码中失败了

public Func<Q, string> GetSortProperty<Q>(IQueryable<Q> data, string SortColumn) 
{
    if (!string.IsNullOrWhiteSpace(sortColumn))
    {
        Func<Q, string> sort = i => i.GetType().GetProperty(sortColumn).GetValue(i, null);

        return sort;
    }

    return null;
}
public Func GetSortProperty(IQueryable数据,字符串SortColumn)
{
如果(!string.IsNullOrWhiteSpace(sortColumn))
{
Func sort=i=>i.GetType().GetProperty(sortColumn).GetValue(i,null);
返回排序;
}
返回null;
}
我审问了以下问题


请帮助我。

基于
GetValue
声明:

public virtual object GetValue(object obj, object[] index);
您可以将方法的类型从
Func
更改为
Func

另外,如果您正在使用C#6,我建议您这样更改它,以防止出现
NullReferenceException

Func<Q, string> sort = i => (string)i.GetType().GetProperty(SortColumn)?.GetValue(i, null);
Func sort=i=>(字符串)i.GetType().GetProperty(SortColumn)?.GetValue(i,null);

基于
GetValue
声明:

public virtual object GetValue(object obj, object[] index);
您可以将方法的类型从
Func
更改为
Func

另外,如果您正在使用C#6,我建议您这样更改它,以防止出现
NullReferenceException

Func<Q, string> sort = i => (string)i.GetType().GetProperty(SortColumn)?.GetValue(i, null);
Func sort=i=>(字符串)i.GetType().GetProperty(SortColumn)?.GetValue(i,null);