代码参考如下:
@{ List<Tuple<string, int>> countList = new List<Tuple<string, int>>(); IEnumerable<dynamic> columnList = Html.GetColumnList(c => c.ParentId == 4348);//获取当前栏目下的子栏目 int publishCount = 0, countIdx = 1; foreach (var item in columnList) { int columnId = item.Id;//栏目id string name = item.Name; var countData = Html.SqlQueryFirst("select count(id) as co from images where columnId=@columnId", new { columnId = columnId }); publishCount = countData.co; countList.Add(new Tuple<string, int>(item, publishCount)); } //按大到小排序 countList.Sort((a, b) => b.Item2.CompareTo(a.Item2)); <ul> @foreach (var item in countList) { <li> <i>@(countIdx) </i> <strong>@(item.Item1) </strong> <span>@(item.Item2)篇 </span> </li> countIdx++; } </ul> }
注:大数据下排序会有点慢,数据多不建议使用。