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; } } } }