zoukankan      html  css  js  c++  java
  • 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; }
            }
        }
    
    
    
    
    
    
    }
  • 相关阅读:
    遇到容易忘记的问题(二)
    遇到容易忘记的问题(三)
    遇到容易忘记的问题(一)
    UC浏览器input文本框输入文字回车键自动提交
    弹框在当前屏幕完全居中的方法
    遇到的浏览器问题总结
    HTML常用的特殊符号&前端使用的标点符号
    小程序里的页面跳转
    移除所有子视图
    UIView用户事件响应
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/4790438.html
Copyright © 2011-2022 走看看