Linq 动态排序列不';我不在林克工作

Linq 动态排序列不';我不在林克工作,linq,asp.net-core,Linq,Asp.net Core,在linq中,我尝试使用动态排序列创建gridview 有谁能帮我看看下面的代码是什么,为什么不起作用 // i created this function to get column value which i need to sorty by private static string GetReflectedPropertyValue( object subject, string field) { object reflectedValue =

在linq中,我尝试使用动态排序列创建gridview 有谁能帮我看看下面的代码是什么,为什么不起作用

// i created this function to get column value which i need to sorty by 
private static string GetReflectedPropertyValue( object subject, string field)
        {
            object reflectedValue = subject.GetType().GetProperty(field).GetValue(subject, null);
            return reflectedValue != null ? reflectedValue.ToString() : "";
        }
//这是我的网格查询
列表票证=新列表();
//在这里,我确定了上升或下降的排序方向
bool asc=(gridViewInputsVM.SortDirection=“asc”)?真:假;
bool desc=(gridViewInputsVM.SortDirection!=“asc”)?真:假;
IQueryable source=(来自_db.TblTicket中的票证
其中(searchRes.assignTic==1)?ticket.AssignedTo==CurrentuserId:true
其中(searchRes.myTicket==1&&searchRes.forOthers!=1)?ticket.CreatedFor==CurrentuserId:true
排序子句
asc?GetReflectedPropertyValue(票证,“票证”):“”,
//这里我需要得到动态列,我需要按它排序
desc?GetReflectedPropertyValue(票证,“票证”):“”降序//无效
选择新建票证搜索
{
title=(ticket.TicketTitle!=null)?ticket.TicketTitle.ToString():“”,
ticId=ticket.TicketId.ToString()
}).AsQueryable();

我将如何解决这个问题

分部类
ticketsarchreslist
是填写分部方法
CustomSort
的部分。CustomSort接受属性名称和排序方向,并使用反射对命名属性进行排序。到目前为止,应该很容易理解

public partial class TicketSearchResList : List<TicketSearchRes>
{
    partial void CustomSort(string propertyName, string direction);

    public void Dump()
    {
        CustomSort("TicketTitle", "desc");

        foreach(var ticket in this)
            Console.WriteLine(ticket.ToString()); // For demo purposes
    }
}


public partial class TicketSearchResList {

    private string propertyName;
    private string direction;

    partial void CustomSort(string propertyName, string direction)
    {
        this.propertyName = propertyName;
        this.direction = direction;
        Sort(Comparer);
    }

    private int Comparer(TicketSearchRes x, TicketSearchRes y)
    {
        int directionChanger = direction == "asc" ? 1 : -1;

        try
        {
            PropertyInfo lhs = x.GetType().GetProperty(propertyName);
            PropertyInfo rhs = y.GetType().GetProperty(propertyName);     

            object o1 = lhs.GetValue(x, null);
            object o2 = rhs.GetValue(y, null);

            if(o1 is IComparable && o2 is IComparable)
            {
                return ((IComparable)o1).CompareTo(o2) * directionChanger;
            }

            // No sort
            return 0;
        }
        catch(Exception ex)
        {
            Debug.WriteLine(ex.Message); // Should log something
            return 0;
        }
    }
公共部分类票证搜索列表:列表
{
部分无效自定义排序(字符串属性名称、字符串方向);
公共空转储()
{
自定义排序(“tickettle”、“desc”);
foreach(本文件中的var票据)
Console.WriteLine(ticket.ToString());//用于演示
}
}
公共部分类票务列表{
私有字符串propertyName;
私有字符串方向;
部分无效自定义排序(字符串属性名称,字符串方向)
{
this.propertyName=propertyName;
这个方向=方向;
排序(比较器);
}
专用整数比较器(票证查询x、票证查询y)
{
int directionChanger=方向==“asc”?1:-1;
尝试
{
PropertyInfo lhs=x.GetType().GetProperty(propertyName);
PropertyInfo rhs=y.GetType().GetProperty(propertyName);
对象o1=lhs.GetValue(x,null);
对象o2=rhs.GetValue(y,null);
if(o1是可比的&&o2是可比的)
{
返回((i可比较)o1)。与(o2)*方向变换器比较;
}
//没有种类
返回0;
}
捕获(例外情况除外)
{
Debug.WriteLine(ex.Message);//应该记录一些内容
返回0;
}
}
使用比较器方法中的反射进行比较。方向为 用于确定是将结果乘以1还是-1。
CompareTo
返回 一个整数,其中-1表示小于,0表示等于,1表示大于 将结果乘以-1,就改变了排序的方向

最后,
ticketsarchreslist
类继承自
List

另外,请看一下Microsoft提供的
Sort
方法

public partial class TicketSearchResList : List<TicketSearchRes>
{
    partial void CustomSort(string propertyName, string direction);

    public void Dump()
    {
        CustomSort("TicketTitle", "desc");

        foreach(var ticket in this)
            Console.WriteLine(ticket.ToString()); // For demo purposes
    }
}


public partial class TicketSearchResList {

    private string propertyName;
    private string direction;

    partial void CustomSort(string propertyName, string direction)
    {
        this.propertyName = propertyName;
        this.direction = direction;
        Sort(Comparer);
    }

    private int Comparer(TicketSearchRes x, TicketSearchRes y)
    {
        int directionChanger = direction == "asc" ? 1 : -1;

        try
        {
            PropertyInfo lhs = x.GetType().GetProperty(propertyName);
            PropertyInfo rhs = y.GetType().GetProperty(propertyName);     

            object o1 = lhs.GetValue(x, null);
            object o2 = rhs.GetValue(y, null);

            if(o1 is IComparable && o2 is IComparable)
            {
                return ((IComparable)o1).CompareTo(o2) * directionChanger;
            }

            // No sort
            return 0;
        }
        catch(Exception ex)
        {
            Debug.WriteLine(ex.Message); // Should log something
            return 0;
        }
    }