首页 > 学院 > 开发设计 > 正文

关于linq的几个小例子

2019-11-14 13:32:18
字体:
来源:转载
供稿:网友
 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();        }

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表