nmeuleman
nmeuleman

Reputation: 284

Getting the lowest price from other table in entity

im working on this website where i bring all the available products on a page. Each of this product has his own ProductDetail(s) with colors, sizes , prices, etc.

I want to display the lowest available price for this product.I use a ViewModel with a list of products and at this moment i dont know how i can get the price to this specific product.Its productsdetails is a list aswell. So somehow i should have to get the lowest price of its ProductDetails.

This is my viewmodel:

var model = new ViewModelProductOverview()
        {
            Products = new ProductRepository().GetProductsAvailable(Id, Index, Size),
            Category = new CategoryRepository().Get(Id),
            CurrentPageID = page.HasValue ? page.Value : -1
        };

My view:

@{
            var i = 0;

            foreach (var product in Model.Products)
            {
                var position = "";
                switch (i)
                {
                    case 0: position = "first";
                        break;
                    case 1: position = "second";
                        break;
                    case 2: position = "third";
                        break;
                    default: i = 0; position = "first";
                        break;
                }

            <li class="@position"><a href="@Url.Action("Detail", "Product", new { productseourl = product.Name.ToSeoUrl(Sign.Minus), productid = product.ID })" >
            </a>
                <table>
                    <tr>
                        <td>
                            @product.Name @product.FabricType.Name
                        </td>
                        <td style="text-align: right;">
                            //Its lowest price need to come here
                        </td>
                    </tr>
                    <tr>
                        <td>
                            @product.SubCategory.Name
                        </td>
                        <td style="text-align: right;">
                            //Promotion price
                        </td>
                    </tr>
                </table>
            </li>
                i++;
            }
        }

Upvotes: 0

Views: 325

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273189

Basically,

 var minPrice = allProducts.Details.Select(d => d.Price).Min();

 var cheapestDetail = allProducts.Details.OrderBy(d => d.Price).First();

Upvotes: 1

Related Questions