static void QueryOverStrings()
{
//字符串數組
string[] currentGames = { "Morrowind", "BioShock", "Half Life 2:Episode 1", "The Darkness", "Daxer", "System Shock 2" };
//建造一個查詢表達式,代表數組中多於6個字母的項
var subset = from g in currentGames
where g.Length > 6
orderby g
select g;
foreach (var s in subset)
{
Console.WriteLine("Item:{0}", s);
}
Console.ReadLine();
}
{
//字符串數組
string[] currentGames = { "Morrowind", "BioShock", "Half Life 2:Episode 1", "The Darkness", "Daxer", "System Shock 2" };
//建造一個查詢表達式,代表數組中多於6個字母的項
var subset = from g in currentGames
where g.Length > 6
orderby g
select g;
foreach (var s in subset)
{
Console.WriteLine("Item:{0}", s);
}
Console.ReadLine();
}
通常使用隱式類型來捕獲LINQ的結果集。
延遲執行,可以爲相同的容器多次應用相同的LINQ查詢,始終獲得最新的結果。
static void QueryOverInts()
{
int[] numbers = { 10, 20, 30, 40, 1, 2, 3, 8 };
//獲取小於10的數
var subset = from i in numbers
where i < 10
select i;
foreach (int j in subset)
Console.WriteLine(j);
Console.WriteLine();
numbers[1] = 3;
foreach (int j in subset)
Console.WriteLine(j);
Console.WriteLine();
}
{
int[] numbers = { 10, 20, 30, 40, 1, 2, 3, 8 };
//獲取小於10的數
var subset = from i in numbers
where i < 10
select i;
foreach (int j in subset)
Console.WriteLine(j);
Console.WriteLine();
numbers[1] = 3;
foreach (int j in subset)
Console.WriteLine(j);
Console.WriteLine();
}
可以使用Enumberable獲取查詢的總數
如:
int numb = (from g in currentGames
where g.length>6
order by g
select g).Count<string>();
where g.length>6
order by g
select g).Count<string>();
排除重複對象:
var makes = (from c in Mycars select c.make).Distinct<string>();
使用Enumerable類型的泛型Reverse<T>()
對表達式進行排序使用orderby,默認是升序排序,使用descending表示降序排序
使用Enumberable.Except()方法可以獲得兩個容器之間的不同。
將查詢結構轉換爲Array類型。
var makeColors = from c in myCars select new {c.make,c.color };
return makesColors.ToArray();
從Main()中調用和處理數據:
Array objs=GetPro();