简体   繁体   English

C# foreach 中的排序

[英]C# Order by in foreach

I'm new to MVC C#.我是 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>
}

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.因此, <div>的顺序应该略有不同。

How to modify the code so it works properly?如何修改代码使其正常工作?

The order of the Description field should be ordered by Order column. Description字段的顺序应按Order列排序。

Use OrderBy extension method on IEnumerable in System.Linq namespace在 System.Linq 命名空间中的 IEnumerable 上使用OrderBy扩展方法

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

You can use the Linq OrderBy extension method您可以使用 Linq OrderBy 扩展方法

@foreach (var item in Model.OrderBy(i => i.Order))
{
}

Directly use orderby直接使用orderby

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM