---恢复内容开始---

本文不想罗列linq的通俗使用方法。因为很多博文都已经写得很详细了。

此处直接贴出源码,如果有需要的朋友可以参考,希望更多的朋友能够补充更多的linq的高级应用。

源码如下:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; namespace linq
{
class Program
{
static void Main(string[] args)
{
Person[] ps = {
new Person{Name="jack",Age=,Phone="",PID=},
new Person{Name="tom",Age=,Phone="235asdg64",PID=},
new Person{Name="jerry",Age=,Phone="235dasg64",PID=},
new Person{Name="lily",Age=,Phone="23asdg564",PID=}
};
Animal[] ana = {
new Animal{AID=,Name="dog",PID=},
new Animal{AID=,Name="cat",PID=},
new Animal{AID=,Name="dog",PID=},
new Animal{AID=,Name="chicken",PID=},
new Animal{AID=,Name="dog",PID=},
new Animal{AID=,Name="dog",PID=},
new Animal{AID=,Name="cat",PID=},
new Animal{AID=,Name="cat",PID=}
};
Console.WriteLine("----------------分组排序------------------------");
//分组
var result = from p in ps
group p by p.Age into g//根据p的值对p进行分组,每组放入g变量中
orderby g.Count() descending
//g.Key为每一组的“组标识”(比如根据年龄groupby,年龄就是组标识)
select new { Name = g.Key, Count = g.Count() };
ps.Where(p => p.Age > );
foreach (var p in result)
{
Console.WriteLine("年龄为:"+p.Name + "的人数是:" + p.Count);
} Console.WriteLine("----------------等值连接------------------------");
//连接 多表查询---下面为等值连接即内连接
var data = from p in ps
join a in ana on p.PID equals a.PID
select new {PID=p.PID,PName=p.Name,PAge=p.Age,PPhone=p.Phone,AID=a.AID,AName=a.Name };
foreach (var item in data)
{
Console.WriteLine(item.PID+"--"+item.PName+"--"+item.PAge+"--"+item.PPhone+"--"+item.AID+"--"+item.AName);
}
Console.WriteLine("----------------左连接------------------------");
//左连接的写法
var data1 = from p in ps
join a in ana on p.PID equals a.PID
into temp //临时表
from t in temp.DefaultIfEmpty()
select new { PID = p.PID, PName = p.Name, PAge = p.Age, PPhone = p.Phone, AID =( t!=null?t.AID:), AName = (t!=null?t.Name:"null")};
foreach (var item in data1)
{
Console.WriteLine(item.PID + "--" + item.PName + "--" + item.PAge + "--" + item.PPhone + "--" + item.AID+ "--" + item.AName);
} //lamba本质剖析
var d1 = from p in ps
where p.Age>
select p;
//Where,Select等均为IEnumerable接口的扩展方法
var d2 = ps.Where(p => p.Age > ).Select(p => p);
var d3 = ps.Where(p => p.Age > );
var d4 = ps.Where((p) => p.Age > );
//其实where方法内的参数是一个匿名方法或者说是匿名委托 var p=function(parameter){return i>0}
//系统委托 public delegate TResult Func<in T, out TResult>(T arg);
//当只有一个参数,方法内只有一行代码时可以简写为 var d3 = ps.Where(p => p.Age > 30);
var d5 = ps.Where((p) => { return p.Age > ; }); //lamba在多线程中的应用
//i => { Console.WriteLine("hhh" +i); }就是一个匿名方法或者说匿名委托
//Thread t1 = new Thread(i => { Console.WriteLine("hhh" +i); });
Thread t1 = new Thread((i) => { Console.WriteLine("我是多线程委托实现:" + i); });
t1.Start(); //匿名委托的实现方式
//DelFun dfun=delegate(int i){Console.WriteLine(i);};
DelFun dfun = (i => { Console.WriteLine("我是匿名委托:"+i); });
dfun(); Console.Read();
}
delegate void DelFun(int i);
class Person
{
public int PID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Phone { get; set; }
}
class Animal
{
public int AID { get; set; }
public string Name { get; set; }
public int PID { get; set; } }
}
}
04-28 23:44