lambda linq 表达式 ListBox 的升序 降序 乱序

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 ListBoxDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        List<string> array = new List<string>();

        //初始化
        private void Form1_Load(object sender, EventArgs e) {
            string[] arr = { "a.m.", "a", "Smith", "Jones", "module", "zoology",
                                "youth", "Hsieh", "abdomen", "Xerox", "season",
                                "Luncheon", "transferable","macro","genuine" };
            array.AddRange(arr);

            listBox1.DataSource = array;

        }

        private void button升序_Click(object sender,EventArgs e) {
            //lambda
            var a = array.OrderBy(c => SortOrder.Ascending).ToArray();

            //linq
            //var a = from x in array orderby x ascending select x;
            listBox1.DataSource =a;
        }

        private void button降序_Click(object sender,EventArgs e) {
            //lambda
            var b = array.OrderBy(c => SortOrder.Descending).Reverse().ToArray();

            //linq
            //var a = from x in array orderby x descending select x;

            listBox1.DataSource = b;
        }

        private void button乱序_Click(object sender,EventArgs e) {
            //lambda
            //var a = array.OrderBy(c => Guid.NewGuid()).ToArray();

            //linq
            var a = from x in array orderby Guid.NewGuid()  select x;
            listBox1.DataSource = a.ToArray();
        }
    }
}
(0)

相关推荐