简体   繁体   English

获取列表项中的最低/最高价格

[英]Get the lowest/biggest price within an item of a List

The goal 目标

Highlight (on HTML) the item whose its price it is the lowest or biggest of a list of items. 高亮显示(在HTML上)价格最低的商品。

The scenario 场景

There is a List<Products> on my application. 我的应用程序上有一个List<Products> Four items of this list are illustrated by: 此列表中的四个项目说明如下:

Item 01 项目01

  • Product Name: Xbox 360 产品名称:Xbox 360
  • Market Name: Bestbuy 市场名称:Bestbuy
  • Price: US$159,90 价格:US $ 159,90

Item 02 项目02

  • Product Name: Xbox 360 产品名称:Xbox 360
  • Market Name: Walmart 市场名称:沃尔玛
  • Price: US$129,90 价格:US $ 129,90

Item 03 项目03

  • Product Name: Xbox 360 产品名称:Xbox 360
  • Market Name: eBay 市场名称:eBay
  • Price: US$125,00 价格:US $ 125,00

Item 04 项目04

  • Product Name: Xbox 360 产品名称:Xbox 360
  • Market Name: Amazon 市场名称:亚马逊
  • Price: US$119,90 价格:US $ 119,90

As you can see, there is a biggest price as well as a lowest price — and I want to highlight them on HTML with some classes. 如您所见,既有最高价也有最低价-我想用一些类在HTML上突出显示它们。

Code spotlight 代码焦点

The markup is: 标记是:

<div class="body">
    <table class="table table-bordered table-hover">
        @foreach (var item in Model.ProductList)
        {
            <tr>
                <td><img src="@item.marketName" /></td>
                <td>US$@item.productPrice</td>
            </tr>
        }
    </table>
</div>

The foreach above iterates with the list ( Model.ProductList ) that I've mentioned before. 上面的foreach迭代了我之前提到的列表( Model.ProductList )。

The question 问题

With all those information, how can I get the lowest/biggest price of some item of this foreach ? 有了所有这些信息,我如何才能获得此foreach中某些item的最低/最高价格?

What I've already tried 我已经尝试过的

I have already tried this: 我已经尝试过了:

@Model.ProductList.Min()

But this fragment of code doesn't make sense — is it to get the min of what? 但是这段代码没有意义- 是为了获得最低限度的收益吗? . Maybe something like this (of course doesn't work, but just to illustrate): 也许是这样的(当然不起作用,只是为了说明):

@Model.ProductList.productPrice.Min()

Ambiguous question? 模棱两可的问题?

I searched on Google and StackOverflow about this subject, but without success — no results found. 我在Google和StackOverflow上搜索了有关此主题的内容,但没有成功-未找到结果。

您需要将Lambda表达式传递给Min()来计算最小化的值:

Model.ProductList.Min(p => p.ProductPrice)

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

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