• 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)
            {
    
            }
        }
    }
  • 相关阅读:
    蒙特卡洛采样、重要性采样
    伯努利分布和高斯分布下的最大似然估计、交叉熵
    对于分类问题的神经网络最后一层的函数:sigmoid、softmax与损失函数
    android 侧滑菜单
    安卓Animation类与xml制作动画
    LeetCode题解 #3 Longest Substring Without Repeating Characters
    GY89的使用
    使用GY89的BMP180模块获取温度和压强(海拔)
    STM32与PC机串口通讯
    STM32使用无源蜂鸣器演奏歌曲
  • 原文地址:https://www.cnblogs.com/yinyitianya/p/5714509.html
Copyright © 2020-2023  润新知