C#查询表达式

程序使用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)
        {

        }
    }
}

 

转载于:https://www.cnblogs.com/yinyitianya/p/5714509.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值