C#中Linq的基本概念和使用


using System;
using System.Linq;
using System.Collections.Generic;

namespace Linq
{
    class Program
    {
        static void Main(string[] args)
        {
            // BasicConcept();
            // QuerySyntax();
            QueryOperations();

            Console.ReadLine();
        }

        private static void BasicConcept()
        {
            // Linq: Lanuage Intergarted Query 语言整合查询
            // select * from tableName
            // 实现IEnumerable 都可以用Linq语句查询
            // LINQ to SQL, LINQ to XML, LINQ to DataSet, LINQ to Objects

            int[] numbers = { 5, 10, 8, 3, 6, 12 };

            // 1.Query syntax
            var numQurey1 = from num in numbers
                            where num % 2 == 0 // 余数等于0
                            orderby num // 按大小排序
                            select num;

            // 2.Method syntax
            var numQuery2 = numbers.Where(n => n % 2 == 0)
                                   .OrderBy(n => n);

            foreach (var i in numQurey1)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine();
            foreach (var i in numQuery2)
            {
                Console.Write(i + " ");
            }
        }

        private static void QuerySyntax()
        {
            // 1, Data Source
            // IEnumerble
            int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7 };

            // 2, Query creation
            var numQuery = from num in numbers
                           where num % 2 == 0
                           select num;

            int numCount = numQuery.Count();
            numQuery.ToList();
            numQuery.ToArray();

            // 3, Query execution
            foreach (var num in numQuery)
            {
                Console.Write("{0,1} ",num);
            }
        }

        private static void QueryOperations()
        {
            int[] numbers = { 0, 1, 2, 4, 6, 7 };

            var query = from num in numbers // 获取数据源
                        where num % 2 == 1 || num % 3 == 1 // 过滤条件
                        orderby num ascending // 升序 ascending 降序 descending
                        select num;

            foreach (var num in query)
            {
                Console.Write("{0} ", num);
            }
            Console.WriteLine();

            List customers = new List();
            customers.Add(new Customer() { Name = "Jack", City = "Beijing"});
            customers.Add(new Customer() { Name = "LiLei", City = "Beijing"});
            customers.Add(new Customer() { Name = "WangMeimei", City = "Shanghai"});

            List employees = new List();
            employees.Add(new Employee() { Name = "Jack", ID = 101});
            employees.Add(new Employee() { Name = "Emily", ID = 102});

            var queryCustomers = from c in customers
                                 group c by c.City into curGroup // 根据城市 分组
                                 where curGroup.Count() >= 2 // into 可以再对分组结果进行过滤
                                 select new { City = curGroup.Key, Number = curGroup.Count() };

            foreach (var c in queryCustomers)
            {
                Console.WriteLine("{0} Count {1}", c.City, c.Number);
            }

            //foreach (var cg in queryCustomers)
            //{
            //    Console.WriteLine(cg.Key);
            //    foreach (var c in cg)
            //    {
            //        Console.WriteLine(" {0}", c.Name);
            //    }
            //}

            var queryJoin = from c in customers
                            join e in employees on c.Name equals e.Name
                            select new { PersonName = c.Name, PersonID = e.ID, PersonCity = c.City };

            foreach (var p in queryJoin)
            {
                Console.WriteLine("{0} {1} {2}", p.PersonName, p.PersonID, p.PersonCity);
            }

            string[] strings = { "Hello jikexueyuan.", "This is Friday!", "Are you happy?" };
            var stringQuery = from s in strings
                              let words = s.Split(' ') // let 中间变量的作用
                              from word in words
                              let w = word.ToUpper()
                              select w;

            foreach (var s in stringQuery)
            {
                Console.WriteLine("{0} ", s);
            }

        }
    }

    class Customer
    { 
        public string Name
        {
            get;
            set;
        }

        public string City
        {
            get;
            set;
        }
    }

    class Employee
    {
        public string Name { get; set; }
        public int ID { get; set; }
    }
}

C