我正在尝试浏览一系列运输方式,然后将它们作为选项设置在表格中。问题是我的运输方式被打乱了,我们不想在我们的数据库中重新排列它。这意味着我需要遍历所有元素,但按照以下 shipmethodID 的顺序:11、13、12。
以下是代码片段:
@foreach (var shippingMethod in availableShippingMethods)
{
bool chosen = false;
if (choosenShippingMethod != null)
{
if (choosenShippingMethod.Id == shippingMethod.Id)
{
chosen = true;
}
}
else
{
chosen = counter == availableShippingMethods.Count();
}
<td>
... some html here ...
</td>
}
最佳答案
在您的数据库表中创建一个名为 sorting_order
的列,从您的查询或 View 中对该列进行排序,您就完成了。灵活、可重复使用、检查。
永远不要依赖永不改变的 ID 或其他奇怪的逻辑。您无法维护此类软件。
关于c# - foreach 循环,其中顺序需要特定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46545421/