Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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# 使用自定义实体数据绑定的排序问题_C#_Sorting_Entity - Fatal编程技术网

C# 使用自定义实体数据绑定的排序问题

C# 使用自定义实体数据绑定的排序问题,c#,sorting,entity,C#,Sorting,Entity,使用自定义实体类对Gridview进行排序时出现问题 我的复杂实体 public class Subscription { public string SubscriptionID { get; set; } public string Status { get; set; } public Customer { get; set; } //Contained class from another entity } public class Customer {

使用自定义实体类对Gridview进行排序时出现问题

我的复杂实体

public class Subscription
{

    public string SubscriptionID { get; set; }
    public string Status { get; set; }
    public Customer { get; set; } //Contained class from another entity
}


public class Customer
{
    public string CustomerID { get; set; }
    public CustomerName { get; set; }
}
绑定到gridview

 List<Subscription> subscriptions = GetSubscriptions();


 //sorting, sortEvent is the GridviewSortEvenArgs param
 subscriptions.Sort(new GenericComparer<Subscription>(sortEvent.SortExpression, GridViewSortDirection));



 grdSubscription.DataSource = subscriptions;
 grdSubscription.DataBind();

请提前告知,谢谢

您可以尝试的一个快速而肮脏的方法是将CustomerName属性添加到您的Subscription类中。在使用Silverlight/WCF/EF时,我通常在客户端代码分部类中这样做

public string CustomerName 
{ get 
   { return Customer != null ? Customer.CustomerName : string.empty }
}

然后从您的UI引用此属性。

如何绑定排序?是的,我可以简单地添加此属性,但它将提升redunduncy,因为我的Customer类中已经有CustomerName。。。我将实体设计为实际数据库的规范化表。。谢谢当我想消除我的数据问题在哪里的问题时,我会使用这个。例如,在EF中,有时订阅对象没有深度加载,例如,除非.IncludeCustomer在查询方法中,否则不会创建Customer实例。解决这些问题后,您可以删除冗余属性,尽管Subscription类的用户可能会喜欢helper方法,而不必总是对Subscription.Customer.CustomerName进行编码
     <asp:TemplateField HeaderText="Subscription ID" SortExpression="SubscriptionID">
       <asp:TemplateField HeaderText="Customer Name" SortExpression="CustomerName">
                <ItemTemplate>
                   <%# Eval(" Customer.CustomerName ") %>
                </ItemTemplate>
   public class GenericComparer<T> : IComparer<T>
{
    private SortDirection sortDirection;

    public SortDirection SortDirection
    {
        get { return this.sortDirection; }
        set { this.sortDirection = value; }

    }

    private string sortExpression;

    public GenericComparer(string sortExpression, SortDirection sortDirection)
    {
        this.sortExpression = sortExpression;
        this.sortDirection = sortDirection;
    }

    public int Compare(T x, T y)
    {
        PropertyInfo propertyInfo = typeof(T).GetProperty(sortExpression);

        IComparable obj1 = (IComparable)propertyInfo.GetValue(x, null);
        IComparable obj2 = (IComparable)propertyInfo.GetValue(y, null);

        if (SortDirection == SortDirection.Ascending)
        {
            return obj1.CompareTo(obj2);
        }

        else return obj2.CompareTo(obj1);
    }
}
     protected SortDirection GridViewSortDirection
{
    get
    {
        // Checks for the first time when the ViewState sort direction is null
        if (ViewState["sortDirection"] == null)
            ViewState["sortDirection"] = SortDirection.Ascending;
        // Changes the sort direction
        else
        {
            if (((SortDirection)ViewState["sortDirection"]) == SortDirection.Ascending)
            {
                ViewState["sortDirection"] = SortDirection.Descending;
            }
            else
            {
                ViewState["sortDirection"] = SortDirection.Ascending;
            }
        }
        return (SortDirection)ViewState["sortDirection"];
    }
    set
    {
        ViewState["sortDirection"] = value;

    }
}
public string CustomerName 
{ get 
   { return Customer != null ? Customer.CustomerName : string.empty }
}