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 = });
Customers.Add(new Customer() { CustomerName = "Li2", CityID = });
Customers.Add(new Customer() { CustomerName = "Li3", CityID = });
Customers.Add(new Customer() { CustomerName = "Li4", CityID = }); List<City> Citys = new List<City>();
Citys.Add(new City() { CityID = , CityName = "北京" });
Citys.Add(new City() { CityID = , CityName = "上海" });
Citys.Add(new City() { CityID = , CityName = "广州" });
Citys.Add(new City() { CityID = , CityName = "深圳" }); #region 简单查询
//1.Query systax
var query = from c in Customers
where c.CityID % ==
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 % == ).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() >
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() > ).
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; }
}
} }
上一篇:[转]JQuery.Ajax之错误调试帮助信息


下一篇:Ubuntu执行命令时,不sudo提示权限不足,sudo提示找不到该命令