简体   繁体   中英

Finding Min and Max using LINQ

I am trying to create a LINQ query that allows the user to select Either Min Or Max from a drop down that I have on my web page. If the user selects Min it will display the record with the lowest Quote (Quote coming from a database table called rentals)

I have not had any luck with.Min() and.Max(). I am not sure how to go about this so any help would be appreciated.

This is my if statement for deciding what option the user has chosen on the drop down box:

namespace CarGarageSales.Pages.Queries
{
    [BindProperties]
    public class Query3Model : PageModel
    {
        private readonly CarGarageSales.Data.CarGarageSalesContext _context;

        public IList<Rental> Rental { get; set; }

        [BindProperty(SupportsGet = true)]

        public int UserInput { get; set; }

        public Query3Model(CarGarageSales.Data.CarGarageSalesContext context)
        {
            _context = context;
        }

        public async Task OnGetAsync()
        {

            if (UserInput == 0)
            {
                var Rentals = (from s in _context.Rentals
                               select s);

                Rental = await Rentals.ToListAsync();
            }
            else if (UserInput == 1)
            {
                var Rentals = (from s in _context.Rentals
                               select s).Min();

            }
            else
            {
                var Rentals = (from s in _context.Rentals
                               select s.Quote).Max();
            }
        }
    }
}

This is my HTML section:

@page
@model CarGarageSales.Pages.Queries.Query3Model
@{
    ViewData["Title"] = "Query3";
}

<h2>Query3</h2>


<form>
    <p>
        Min or Max?:<select asp-for="UserInput">
       <option></option>    
       <option>Min</option>
       <option>Max</option>
    </select>

        <input type="submit" value="Search" />
    </p>
</form>


<p class="Text">Here is the record for the Rentals you requested!</p>

<table class="table Text">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Rental[0].RentalID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Rental[0].Price)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Rental[0].Duration)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Rental[0].Quote)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Rental)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.RentalID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Price)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Duration)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Quote)
                </td>
            </tr>
        }
    </tbody>
</table>

Rentals class:

namespace CarGarageSales.Models
{
    [BindProperties]
    public class Rental
    {
        [Required]
        public int RentalID { get; set; }

        [Required]
        [Display(Name = "Price")]
        [Range(100, 200000, ErrorMessage = "Price must be between 100 and 200,000 Pounds")]
        public decimal Price { get; set; }

        [Required]
        [Display(Name = "Duration")]
        [Range(1, 36,ErrorMessage = "Duration must be between 1 and 36 Months")]
        public int Duration { get; set; }

        [Required]
        [Display(Name = "Quote")]
        [Range(20, 10000, ErrorMessage = "Quote must be between 20 and 10000 Pounds")]
        public decimal Quote { get; set; }

        public Customer Customer { get; set; }

        public virtual IList<Car> Cars { get; set; }
        public virtual IList<SalesManRental> SalesManRental { get; set; }
    }
}

Let's see an example below ( Here is your live code )

using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var option = 1;
        var list = new[]{ new Rental{ RentalID = 1,  Quote = 1 }, new Rental{ RentalID = 2,  Quote = 2 }, new Rental{ RentalID = 3,  Quote = 3 }, new Rental{ RentalID = 4,  Quote = 1 }, new Rental{ RentalID = 5,  Quote = 3 } };

        var minQuote = list.Min((p => p.Quote));        
        var maxQuote = list.Max((p => p.Quote));
        var result = list.Where(p => (option == 1 && p.Quote == minQuote) || (option == 2 && p.Quote == maxQuote));
        if(option == 0)
            result = list; 

        foreach(var item in result)
                Console.WriteLine(item.Quote);
    }


    public class Rental
    {
        public int RentalID { get; set; }

        public decimal Price { get; set; }

        public decimal Quote { get; set; }
    }
}

Note: Change value of option in range 0, 1, 2 to test your case.

You last example within the else should work to return the max Quote , as long as you are sure have data in _context.Rentals . I am not sure why you are using Async Task but you are not awaiting anything within UserInput == 1 or the else , only UserInput == 0

This should work

var Rentals = (from s in _context.Rentals select s.Quote).Max();

Example (test it here https://dotnetfiddle.net/2SnPcS )

using System;
using System.Collections.Generic;
using System.Linq;

public class Program {
    public class Rental
    {
        public int RentalID { get; set; }
        public decimal Price { get; set; }
        public int Duration { get; set; }
        public decimal Quote { get; set; }
    }

    public static void Main()
    {
        // sample data
        var rentals = new List<Rental> {
            new Rental { RentalID = 1, Price = 100.00m, Duration = 2, Quote = 200.00m },
            new Rental { RentalID = 2, Price = 100.00m, Duration = 2, Quote = 200.00m },
            new Rental { RentalID = 3, Price = 100.00m, Duration = 1, Quote = 100.00m },
            new Rental { RentalID = 4, Price = 100.00m, Duration = 1, Quote = 100.00m },
            new Rental { RentalID = 5, Price = 100.00m, Duration = 5, Quote = 500.00m }
        };

        // get min quote
        var minQuote = (from s in rentals select s.Quote).Min();

        // get max quote
        var maxQuote = (from s in rentals select s.Quote).Max();

        Console.WriteLine(string.Format("min={0} max={1}", minQuote, maxQuote));

        // Outputs: min=100.00 max=500.00
    }
}

I am not sure what the rest of your application looks like or how it is being used, but yes you can call your methods anything you want. Do you want just the Quote decimal value, or the entire Rental associated with it? To change your OnGetAsync method based on what you showed us, getting the decimal value only, it would look something like this (you were almost there).

public decimal GetQuoteAmount()
{
    // this could be min
    if (UserInput == 0)
    {
        return (from s in _context.Rentals select s.Quote).Min();
    }

    // this could be max
    else if (UserInput == 1)
    {
        return (from s in _context.Rentals select s.Quote).Max();
    }
    else 
    {
        // is there a third option? What do you want to happen if they do not select Min or Max?
        return 0;
    }
}

Not tested, but you could do something like this:

        var filteredRentals = _context.Rentals.GroupBy(g => g.RentalID)
            .Select(x => new Rental
            {
                RentalID = x.Key,
                Quote = x.Max(z => z.Quote),
                Duration = x.FirstOrDefault(r => r.RentalID == x.Key && r.Quote == x.Max(z => z.Quote)).Duration,
                Price = x.FirstOrDefault(r => r.RentalID == x.Key && r.Quote == x.Max(z => z.Quote)).Price
            }).ToList();

The idea is you group it by RentalID to get the Max/Min Quote , then get the first record that has those RentalID and Quote

In case they have same quote and you want to show all records instead of the first:

        var groupedRentals = _context.Rentals.GroupBy(g => g.RentalID)
            .Select(x => new
            {
                RentalID = x.Key,
                Quote = x.Max(z => z.Quote)
            }).ToList();

        var filteredGroupedRentals = _context.Rentals.Where(r => groupedRentals.Any(g =>
            g.RentalID == r.RentalID &&
            g.Quote == r.Quote))
            .ToList();

As others have mentioned, you'll need to use .Select(s => s.Quote).Min() (or .Max ). However, your select list options don't have any values. You need to specify the appropriate values, such as:

<option value="0">All</option>
<option value="1">Min</option>
<option value="2">Max</option>

I think part of the problem is that you only assign Rental in the case where UserInput == 0 . Adding an assignment in the other cases should make your results update when the user changes their input.

Additionally, it sounds like you want to get the full records of all the objects with the largest (or smallest) Quote . If this is the case, then the OrderBy and GroupBy methods should help here because they allow you to sort the data based on it's properties, like the value of Quote , and group data with the same value together.

For example:

public async Task OnGetAsync()
{
    if (UserInput == 0)
    {
        Rental = _context.Rentals().ToListAsync();
    }
    else if (UserInput == 1)
    {
        // Get the group of items with min quote    
        Rental = _context.Rentals()
            .GroupBy(r => r.Quote) // Group items with the same quote (Quote becomes the Key)
            .OrderBy(g => g.Key)   // Order the groups by quote (smallest will be first)
            .FirstOrDefault()      // Choose the first group (those with min quote)
            .ToListAsync();        // Select the items into a list
    }
    else
    {
        // Get the group of items with max quote    
        Rental = _context.Rentals()
            .GroupBy(r => r.Quote) // Group items with the same quote (Quote becomes the Key)
            .OrderBy(g => g.Key)   // Order the groups by quote (smallest will be first)
            .LastOrDefault()       // Choose the last group (those with max quote)
            .ToListAsync();        // Select the items into a list
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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