LINQ 常用from
單個form子句
string[] values = { "LINQ學習", "LINQ基本語句", "from子句", "單個from子句" };
var value = from v in values
where v.IndexOf("LINQ") > -1 //包含字符串
select new { v, v.Length };//返回自定義數組
foreach (var n in value)
{
Response.Write(n.v+"###"+n.Length);
使用LINQ查詢ArrayList
ArrayList gList = new ArrayList();
gList.Add(new GustInfo { Name="DebugLZQ", Age=26, Tel="88888888"});
gList.Add(new GustInfo { Name="博客園",Age=6, Tel ="666666"});
gList.Add(new GustInfo { Name = "M&MSoft", Age =9, Tel = "55555" });
var query = from GustInfo gust in gList
where gust.Age > 9
select gust;//範圍變量gust制定了數據類型
foreach (GustInfo g in query)
{
Console.WriteLine("{0} 年齡:{1} 電話:{2}",g.Name,g.Age,g.Tel );
}
//復合from子句
{
new GustInfo1{ Name="DebugLZQ",Age=26,TelTable=new List<string>(){"8888888","138******"}},
new GustInfo1{ Name="博客園",Age=6,TelTable =new List<string>(){"666666","138******"}},
new GustInfo1{ Name="M&MSoft",Age=9,TelTable=new List<string>(){"55555","138******"}}
};
//gust、tel都是查詢變量,作用域為當前查詢語句!!!
var query2 = from gust in gList2
from tel in gust.TelTable
where tel.IndexOf("8888888") > -1
select gust;
foreach (var g in query2)
{
Response.Write(g.Name+" 年齡"+ g.Age);
foreach (var t in g.TelTable)
{
Response.Write("電話:"+t);
}
}
//4多個from子句
ArrayList gList = new ArrayList();
gList.Add(new GustInfo { Name = "DebugLZQ", Age = 26, Tel = "88888888" });
gList.Add(new GustInfo { Name = "博客園", Age = 6, Tel = "666666" });
gList.Add(new GustInfo { Name = "M&MSoft", Age = 9, Tel = "55555" });
List<GustInfo1> gList2 = new List<GustInfo1>()
{
new GustInfo1{ Name="DebugLZQ",Age=26,TelTable=new List<string>(){"8888888","138******"}},
new GustInfo1{ Name="博客園",Age=6,TelTable =new List<string>(){"666666","138******"}},
new GustInfo1{ Name="M&MSoft",Age=9,TelTable=new List<string>(){"55555","138******"}}
};
//4多個from子句
var query3 = from GustInfo gust in gList
where gust.Age > 6
from GustInfo1 gust2 in gList2
where gust2.Age > 9
select new { gust, gust2 };//查詢結果定制
foreach (var g in query3)
{
Response.Write(g.gust.Name + "###" + g.gust2.Name);
}
LINQ 常用from