C#的LINQ to Object

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Linq;

namespace codeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //linq : Lauage Intergarted Query
            //继承IEnumerable的类,都可以使用linq
            //Linq to sql,Linq to xml,Linq to DataSet,Linq to objects
            //linq语句在具体调用时,才会执行 toList() Count()

            List<Customer> Customers = new List<Customer>();
            Customers.Add(new Customer() { CustomerName = "Li1", CityID = 1 });
            Customers.Add(new Customer() { CustomerName = "Li2", CityID = 2 });
            Customers.Add(new Customer() { CustomerName = "Li3", CityID = 3 });
            Customers.Add(new Customer() { CustomerName = "Li4", CityID = 3 });

            List<City> Citys = new List<City>();
            Citys.Add(new City() { CityID = 1, CityName = "北京" });
            Citys.Add(new City() { CityID = 2, CityName = "上海" });
            Citys.Add(new City() { CityID = 3, CityName = "广州" });
            Citys.Add(new City() { CityID = 4, CityName = "深圳" });

            #region 简单查询
            //1.Query systax
            var query = from c in Customers
                        where c.CityID % 2 == 0
                        orderby c.CityID descending
                        select c;


            foreach (var item in query)
            {
                Console.WriteLine("{0},{1}", item.CustomerName, item.CityID);
            }

            Console.WriteLine();

            //2.Method systax
            var query2 = Customers.Where(c => c.CityID % 2 == 0).OrderByDescending(c => c.CityID);
            foreach (var item in query2)
            {
                Console.WriteLine("{0},{1}", item.CustomerName, item.CityID);
            }
            #endregion

            #region Group
            Console.WriteLine("queryGroup");
            //1.Query systax
            var queryGroup = from c in Customers
                             group c by c.CityID;

            foreach (var group in queryGroup)
            {
                Console.WriteLine("{0}", group.Key);
                foreach (var item in group)
                {
                    Console.WriteLine("{0},{1}", item.CustomerName, item.CityID);
                }
            }

            Console.WriteLine();

            //2.Method systax
            var queryGroup2 = Customers.GroupBy(c => c.CityID);
            foreach (var group in queryGroup2)
            {
                Console.WriteLine("{0}", group.Key);
                foreach (var item in group)
                {
                    Console.WriteLine("{0},{1}", item.CustomerName, item.CityID);
                }
            }
            #endregion

            #region into
            Console.WriteLine("into");
            //1.Query systax
            var queryGroupinto = from c in Customers
                                 group c by c.CityID into cGroup
                                 where cGroup.Count() > 1
                                 select new { CityID = cGroup.Key, Cout = cGroup.Count() };

            foreach (var group in queryGroupinto)
            {
                Console.WriteLine("{0},{1}", group.CityID,group.Cout);
            }

            Console.WriteLine();

            //2.Method systax
            var queryGroupinto2 = Customers.GroupBy(c => c.CityID).
                Where(cGroup => cGroup.Count() > 1).
                Select(cGroup => new { CityID = cGroup.Key, Cout = cGroup.Count() });
            foreach (var group in queryGroupinto2)
            {
                Console.WriteLine("{0},{1}", group.CityID, group.Cout); 
            }
            #endregion

            #region join
            Console.WriteLine("join");
            //1.Query systax
            var queryJoin = from c in Customers
                            join ci in Citys on c.CityID equals ci.CityID
                            select new { CustomerName = c.CustomerName, c.CityID, ci.CityName };
            foreach (var item in queryJoin)
            {
                Console.WriteLine("{0},{1},{2}", item.CustomerName, item.CityID,item.CityName);
            }
            Console.WriteLine();
            //2.Method systax
            var queryJoin2 = Customers.Join(Citys,
                c => c.CityID,
                ci => ci.CityID,
                (c, ci) => new { CustomerName = c.CustomerName, c.CityID, ci.CityName });

            foreach (var item in queryJoin2)
            {
                Console.WriteLine("{0},{1},{2}", item.CustomerName, item.CityID, item.CityName);
            }
            #endregion

            Console.ReadLine();
        }

        class Customer
        {
            public string CustomerName { get; set; }
            public int CityID { get; set; }
        }

        class City
        {
            public int CityID { get; set; }
            public string CityName { get; set; }
        }
    }






}

 

转载于:https://www.cnblogs.com/lgxlsm/p/4790438.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值