c# - 排序列表项(最新的优先)

标签 c# sharepoint-2010 foreach caml listitem

我无法按正确顺序显示列表项的值。以下是列表中的字段(及其实际系统名称):

-标题 -类别 -提名获奖 -标志

“标题”字段包含年份值,例如“2011”、“2010”等。

//getting the awards list and extracting correct values from the necesary fields
//asp running with elevated privilegs
SPSecurity.RunWithElevatedPrivileges(delegate()
{

using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb())
{



try
{

SPList awardsList = web.Lists["Awards"];

SPListItemCollection listItemCollection = awardsList.Items;



 //Creating the table
 Table tbl = new Table();

 //foreach (SPListItem oListItem in listItemCollection)
 int x = listItemCollectionI.Count;

 for(int i = 0; (i * 2) < x; i++) // divide total item collection by two, each loop 
 iteration, add two awards to a row (left cell, right cell)
 {
 // get listItemCollection[i];
 //Create table rows, table cells

 int leftIndexer = i * 2;
 int rightIndexer = (i * 2) + 1;

 if (leftIndexer == x)
 {
 break;
 }                                

 TableRow tblRow = new TableRow();
 TableCell tblCellLeft = new TableCell(); //for the awards in the first column
 TableCell tblCellRight = new TableCell(); //for the awards in the second column

 tblCellLeft.VerticalAlign = VerticalAlign.Top;
 tblCellLeft.HorizontalAlign = HorizontalAlign.Center;
 tblCellLeft.CssClass = ("style5");

 tblCellRight.VerticalAlign = VerticalAlign.Top;
 tblCellRight.HorizontalAlign = HorizontalAlign.Center;


 // get the values
 awardYear = listItemCollection[leftIndexer]["Title"].ToString();
 awardCategory = listItemCollection[leftIndexer]["Category"].ToString();
 awardNomWon = listItemCollection[leftIndexer]["NominatedWon"].ToString();

 if(listItemCollection[leftIndexer]["Logo"] != null)
 awardLogo = (string)listItemCollection[leftIndexer]["Logo"];

 // add to left cell
 //values for the left column
 tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style1\">" + awardYear + 
 "</div>"));
 tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style2\">" + awardCategory 
 + "</div>"));
 tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style3\">" + awardNomWon + 
 "</div>"));
 tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style4\">" + "<img src=" + 
 awardLogo.Replace(",", "") + "</div>"));

 // add left cell to row
 tblRow.Cells.Add(tblCellLeft);

 if (rightIndexer < x) // if this item exists in the collection (prevent bug with odd 
 number of awards)
 {


 // get the values
 awardYear = listItemCollection[rightIndexer]["Title"].ToString();
 awardCategory = listItemCollection[rightIndexer]["Category"].ToString();
 awardNomWon = listItemCollection[rightIndexer]["NominatedWon"].ToString();

 if (listItemCollection[rightIndexer]["Logo"] != null)
 awardLogo = (string)listItemCollection[rightIndexer]["Logo"];


 // add to right cell
 //Values for the right column
 tblCellRight.Controls.Add(new LiteralControl("<div class=\"style1\">" + awardYear + 
 "</div>"));
 tblCellRight.Controls.Add(new LiteralControl("<div class=\"style2\">" + awardCategory 
 + "</div>"));
 tblCellRight.Controls.Add(new LiteralControl("<div class=\"style3\">" + awardNomWon + 
 "</div>"));
 tblCellRight.Controls.Add(new LiteralControl("<div class=\"style4\">" + "<img src=" + 
 awardLogo.Replace(",", "") + "</div>"));

 // add right cell to row
 tblRow.Cells.Add(tblCellRight);
 }

 // add row to table
 tbl.Rows.Add(tblRow);

 }

 PlaceHolder6.Controls.Add(tbl); // add table outside of loop 
 }
 catch (Exception err)
 {
 PlaceHolder3.Controls.Add(new LiteralControl(err.ToString()));
 }

 }
 }



 });

因此出于某种原因,输出似乎显示了两个最新的列表项,例如列表项“2012”和列表项“2011”显示在底部。理论上,这些应该显示在顶部,然后是列表的其余项。

如有任何建议,我们将不胜感激!

谢谢

最佳答案

您的代码中没有任何内容指示顺序,请使用以下内容:

SPList awardsList = web.Lists["Awards"];                     

SPQuery q = new SPQuery();
q.Query="<OrderBy><FieldRef Name='Title' /></OrderBy>";

SPListItemCollection listItemCollection = awardsList.Items.GetItems(q);   

关于c# - 排序列表项(最新的优先),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8894721/

相关文章:

c# - 枚举的自定义属性是否危险?

c# - 我可以在尝试使用或返回枚举值时避免强制转换它吗?

java - 使用 httpclient\kerberos 以编程方式设置用户名,而不是提示

c# - 如何对来自 checkedlistbox C# 的所有未选中项目进行循环?

loops - Powershell foreach循环在第三次执行时未退出

c# - 以编程方式导出 Azure 模板

c# - 如何将数组列表转换为多维数组

c# - 从 SharePoint 中托管的 WCF 服务访问 SPFarm PropertyBag 时出现 SecurityException

workflow - Sharepoint 工作流程已部署,但未显示

php - 将 foreach 应用于多个数组?