【问题标题】:Use linq to sort on changed field使用 linq 对更改的字段进行排序
【发布时间】:2013-10-28 22:52:16
【问题描述】:

我有一个使用 C# 的 linq 查询,并且想对更改的字段进行排序。该字段是在表中定义为 YYYYMM 的部分日期字段。但是,我希望它显示在我的转发器中, MM/YYYY 但需要将其排序为 YYYYMM。下面是代码,GrantMonthID 是相关字段。中继器按 MM/YYYY 顺序显示数据。

谢谢。

var linqQuery = from nn in grantsReceivedDetailList
                           where (nn.ArrearAuditID == Convert.ToInt32(AdminBasePage.ArrearAuditId))
                            select
                            new
                            {
                                nn.GrantsReceivedID,
                                nn.PayeeMPINumber,
                                Firstname =     participantRepository.GetParticipantDetailsbyMemberParticipantIndex(Convert.ToInt32(nn.PayeeMPINumber)).FirstName + " " +
                                participantRepository.GetParticipantDetailsbyMemberParticipantIndex(Convert.ToInt32(nn.PayeeMPINumber)).LastName,
                                nn.IVANumber,
                                GrantMonthID = nn.GrantMonthID.ToString().Substring(4, 2) + "/" +                                              nn.GrantMonthID.ToString().Substring(0, 4),
                                nn.GrantAmount,
                                nn.Comments
                            };

            linqQuery = linqQuery.OrderByDescending(y => y.GrantMonthID);  
            // Execute the linq query and databind
            grantListRepeater.DataSource = linqQuery;
            grantListRepeater.DataBind(); 

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    只需创建一个要排序的属性和一个要绑定的属性:

    var query = from x in someList
                select new
                {
                    SortField = FormatForSort(x.Field),
                    DisplayField = FormatForDisplay(x.Field)
                };
    
    query = query.OrderBy(x => x.SortField);
    

    或者,按日期选择它,并在转发器中按您想要的方式格式化它,而不是使用 LINQ,因为这更多的是视图问题。

    【讨论】:

    • 很快就联系了,抱歉。此代码更改仍以 MM/YYYY 顺序显示。我需要 YYYYMM 订单。你能详细说明一下这句话吗:
    【解决方案2】:

    添加到将包含原始数据库日期的匿名类型字段:

    var linqQuery = 
        from nn in grantsReceivedDetailList
        where (nn.ArrearAuditID == Convert.ToInt32(AdminBasePage.ArrearAuditId))
        select new {
           nn.GrantsReceivedID,
           nn.PayeeMPINumber,
           Firstname = participantRepository.GetParticipantDetailsbyMemberParticipantIndex(Convert.ToInt32(nn.PayeeMPINumber)).FirstName + " " +
                       participantRepository.GetParticipantDetailsbyMemberParticipantIndex(Convert.ToInt32(nn.PayeeMPINumber)).LastName,
           nn.IVANumber,
           GrantMonthID = nn.GrantMonthID.ToString().Substring(4, 2) + "/" +          
                          nn.GrantMonthID.ToString().Substring(0, 4),
           nn.GrantAmount,
           nn.Comments,
           GrantMonthIdOriginal = nn.GrantMonthID // this field
        };
    

    并按此字段排序:

    linqQuery = linqQuery.OrderByDescending(y => y.GrantMonthIdOriginal);
    
    grantListRepeater.DataSource = linqQuery;
    grantListRepeater.DataBind(); 
    

    【讨论】:

    • 像魅力一样工作。这个网站(和你)摇滚。
    • 我很快就联系上了,抱歉。此代码更改仍以 MM/YYYY 顺序显示。我需要 YYYYMM 订单。谢谢
    • @KentE 不,如果您还有 MM/YYYY 订单,那么您在 OrderByDescending 运算符中选择了错误的文件
    • 没错。做这些东西的时间太多了。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多