[英]Use linq to sort on changed field
我有一個使用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();
只需創建一個要排序的屬性,然后將一個屬性綁定到:
var query = from x in someList
select new
{
SortField = FormatForSort(x.Field),
DisplayField = FormatForDisplay(x.Field)
};
query = query.OrderBy(x => x.SortField);
或者,通過“日期”選擇它,並在中繼器中以所需的方式對其進行格式化,而不是使用LINQ,因為這更多地與View有關。
添加到您的匿名類型字段,其中將包含原始數據庫日期:
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.