# 列表中的最大值和最小值

public class TempDate
{
public double Temp { get; set; }

public DateTime Date { get; set; }
}


private void buttonSubmit_Click(object sender, EventArgs e)
{

DateTime date = dateTimePickerDate.Value.Date;
double temp = double.Parse(textBoxTemp.Text);

TempDate tempDate = new TempDate();

tempDate.Date = date;
tempDate.Temp = temp;

listBoxInfo.Items.Add(date + "\t" + temp + "°C");
}


private void buttonMaxMin_Click(object sender, EventArgs e)
{

DateTime start = dateTimePickerStart.Value.Date;
DateTime end = dateTimePickerEnd.Value.Date;
int highest = 0;
int lowest = 0;

for (int i = 1; i < formMenu.listOfTempDate.Count; i++)
{
{
{
highest = i;
}

{
lowest = i;
}
}
}
}


public partial class FormMenu : Form
{
public List listOfTempDate = new List();

{
InitializeComponent();
}

private void fromCertainRangeToolStripMenuItem_Click(object sender, EventArgs e)
{
FormMaxMinRange formMaxMinRange = new FormMaxMinRange();
formMaxMinRange.Owner = this;
formMaxMinRange.ShowDialog();
}
}


## 最佳答案

List loTempDateList = new List()
{
new TempDate() {Date = DateTime.Now.AddDays(-10), Temp = 10.01 },
new TempDate() {Date = DateTime.Now.AddDays(-5), Temp = 20.01 },
new TempDate() {Date = DateTime.Now.AddDays(-3), Temp = 30.01 },
new TempDate() {Date = DateTime.Now, Temp = 40.01 }
};

var loDateList = loTempDateList.Where(item => item.Date <= ldEnd && item.Date >= ldStart)
.OrderBy(item => item.Temp);

TempDate loMin = loDateList.First();
TempDate loMax = loDateList.Last();

Console.WriteLine("{0}: {1} with max temp", loMax.Date, loMax.Temp);
Console.WriteLine("{0}: {1} with min temp", loMin.Date, loMin.Temp);


9/26/2017 3:17:09 PM: 30.01 with max temp
9/24/2017 3:17:09 PM: 20.01 with min temp


var loDateList = listOfTempDate.Where(item => item.Date <= end && item.Date >= start)
.OrderBy(item => item.Temp);

TempDate loMin = loDateList.FirstOrDefault();
TempDate loMax = loDateList.LastOrDefault();

if (loMin != null && loMax !=  null)
{