0

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(); 
4

2 に答える 2

0

元のデータベースの日付を含む匿名型フィールドに追加します。

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(); 
于 2013-10-28T14:52:16.683 に答える