程序使用vs 2010编写
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace demo33 { public partial class Form1 : Form { int[] numbers = new int[7] { 0, 31, 12, 6, 34, 51, 6 }; public Form1() { InitializeComponent(); for (int i = 0; i < numbers.Length; i++) { listBox1.Items.Add(numbers[i].ToString()); } } private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); //创建查询 var result0 = from num in numbers where (num % 2) == 0 select (num.ToString()); foreach (string num in result0) { listBox1.Items.Add(num); } } private void button3_Click(object sender, EventArgs e) { listBox1.Items.Clear(); var result3 = from num in numbers where num > 2 orderby num descending//从大到小排序 select num; foreach (int num in result3) { listBox1.Items.Add(num); } } private void button4_Click(object sender, EventArgs e) { listBox1.Items.Clear(); var result3 = from num in numbers where num > 2 orderby num ascending //按照ascending方式排序从最小到最大的排序顺序。 //select string.Format("当前项的值为{0}",num) select num; foreach (int num in result3) { listBox1.Items.Add(num); } } private void button5_Click(object sender, EventArgs e) { listBox1.Items.Clear(); int[] numbers = new int[7] { 0, 31, 12, 6, 34, 51, 6 }; int result3 = 0; switch (comboBox1.Text) { case "Max": result3 = (from num in numbers where num > 2 select num).Max(); break; case "Min": result3 = (from num in numbers where num > 2 select num).Min(); break; case "Count": result3 = (from num in numbers select num).Count(); break; default: MessageBox.Show("功能未完善"); break; } listBox1.Items.Add(result3); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } } }