C#在foreach中排序 [英] C# Order by in foreach

查看:534
本文介绍了C#在foreach中排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC C#的新手.

I'm new to MVC C#.

这是现成的工作代码(工作代码的一部分).

Here is a ready and working code (the part of the working code).

@foreach (var item in Model)
{            
    <div style="height: 200px; ">
        @Html.Raw(@item.Description)
    </div>
}

问题是说明"未按正确的顺序显示在页面上.因此,<div> s的顺序应该稍有不同.

The problem is the Description is not displayed on the page in the proper order is. So, the order of <div>s should be slightly different.

如何修改代码以使其正常工作?

How to modify the code so it works properly?

Description字段的顺序应按Order列排序.

The order of the Description field should be ordered by Order column.

推荐答案

在IEnumerable中使用 OrderBy 扩展方法System.Linq命名空间

Use OrderBy extension method on IEnumerable in System.Linq namespace

@foreach (var item in Model.OrderBy(i => i.Order))
{            
    <div style="height: 200px; ">
        @Html.Raw(@item.Description)
    </div>
}

这篇关于C#在foreach中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆