你的位置:首页 > ASP.net教程

[ASP.net教程]关于linq的几个小例子


 private void button1_Click(object sender, EventArgs e)    {      int[] numbers = new int[7] { 0,1,2,3,4,5,6};      var result0 = from num in numbers             where (num % 2 == 0)             select num;      foreach (int n in result0)      {        listBox1.Items.Add(n);      }    }    private void button2_Click(object sender, EventArgs e)    {      listBox1.Items.Clear();      int[] numbers = new int[7] { 2,3,2,4,3,5,5};      var result1 = from num in numbers             where num > 3             orderby num descending             select num;      foreach (int n in result1)      {        listBox1.Items.Add(n);      }    }    private void button3_Click(object sender, EventArgs e)    {      listBox1.Items.Clear();      int[] numbers = new int[7] { 2, 3, 2, 4, 3, 5, 5 };      var result2 = from num in numbers             where num > 3             orderby num ascending             select string.Format("当前值为{0}",num) ;      foreach (string n in result2)      {        listBox1.Items.Add(n);      }    }    private void button4_Click(object sender, EventArgs e)    {      listBox1.Items.Clear();      int[] numbers = new int[7] { 2, 3, 2, 4, 3, 5, 5 };      int result3 = (from num in numbers              where num > 3              select num).Count();      button4.Text += "--" + result3.ToString();    }