Linq的基本用法,代码

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

namespace LinQ
{
    class Program
    {
        static void Main(string[] args)
        {     
            Console.ReadLine();
        }

        #region Linq语句的语法
        private static void BasicConcept()
        {
            int[] numbers = { 5, 10, 8, 3, 6, 12 };
            //Linq 语句的两种语法实现
            //1.Query syntax
            var numQuery1 = from num in numbers
                            where num % 2 == 0
                            orderby num
                            select num;
            //2.Method syntax
            var numQuery2 = numbers.Where(n => n % 2 == 0).OrderBy(n => n);

            foreach (var i in numQuery1)
            {
                Console.WriteLine(i + "");
            }
            Console.WriteLine();
            foreach (var i in numQuery2)
            {
                Console.WriteLine(i + "");
            }
        }
        #endregion


        #region query 的基本组成 Expamle
        private static void QuerySyntax()
        {
            //1.data source
            //必须实现IEnumerble接口
            int[] numbers = { 0, 1, 2, 3, 4, 5, 6 };

            //2.Query creation
            var numquery = from nums in numbers
                           where (nums % 2) == 0
                           select nums;

            //query语句强制执行
            //int temp = numquery.Count();
            List<int> list = numquery.ToList<int>();
            numquery.ToArray<int>();



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

        }
        #endregion
        

        #region Query语句的基本操作
        private static void QueyOperation()
        {
            int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7 };
            var query = from num in numbers
                        where num % 2 == 1 || num % 3 == 1
                        orderby num descending
                        select num;
            foreach (var num in query)
            {
                Console.WriteLine("{0}",num);
            }

            List<Customer> customer = new List<Customer>();
            customer.Add(new Customer() { Name = "Jack", City = "Beijing" });
            customer.Add(new Customer() { Name = "Lilei", City = "Beijing" });
            customer.Add(new Customer() { Name = "Lili", City = "Shanghai" });

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

            var QueryCustomers = from c in customer
                                 group c by c.City;

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


            var queryjoin = from c in customer
                            join e in employees on c.Name equals e.Name
                            select new { PersonName = c.Name, PersonID = e.ID, Persioncity = c.City };
            foreach (var p in queryjoin)
            {
                Console.WriteLine("{0},{1},{2}", p.PersonName, p.PersonID, p.Persioncity);
            }


            //Linq的into的使用
            var QueryCustomers1 = from c in customer
                                  group c by c.City into cusGroup
                                  where cusGroup.Count() >= 2
                                  select new { City = cusGroup.Key, Number = cusGroup.Count() };
            foreach (var c in QueryCustomers1)
            {
                Console.WriteLine("{0} count {1}", c.City, c.Number);
            }

            //let主要处理中间变量
            string[] strings = { "hello hello", "world world", "happy happy" };
            var stringQuery = from s in strings
                              let words = s.Split(' ')
                              from word in words
                              let w = word.ToUpper()
                              select w;
        }
        #endregion      
        
    }


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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值