- 相關(guān)推薦
c#查詢(xún)關(guān)鍵字from 子句的用法
引導(dǎo)語(yǔ):所謂關(guān)鍵字,就是使用的搜索關(guān)鍵詞,技巧的輸入您的關(guān)鍵詞,對(duì)搜索內(nèi)容的準(zhǔn)確有很大的幫助以下是小編整理的c#查詢(xún)關(guān)鍵字from 子句的用法,歡迎參考閱讀!
查詢(xún)表達(dá)式必須以 from 子句開(kāi)頭。另外,查詢(xún)表達(dá)式還可以包含子查詢(xún),子查詢(xún)也是以 from 子句開(kāi)頭。from 子句指定以下內(nèi)容:
將對(duì)其運(yùn)行查詢(xún)或子查詢(xún)的數(shù)據(jù)源。
一個(gè)本地范圍變量,表示源序列中的每個(gè)元素。
范圍變量和數(shù)據(jù)源都是強(qiáng)類(lèi)型。from 子句中引用的數(shù)據(jù)源的類(lèi)型必須為 IEnumerable、IEnumerable<(Of <(t>)>) 或一種派生類(lèi)型(如 IQueryable<(Of <(t>)>))。
在下面的示例中,numbers 是數(shù)據(jù)源,而 num 是范圍變量。請(qǐng)注意,這兩個(gè)變量都是強(qiáng)類(lèi)型,即使使用了 var 關(guān)鍵字也是如此。
C#
class LowNums
{
static void Main()
{
// A simple data source.
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
// Create the query.
// lowNums is an IEnumerable
var lowNums = from num in numbers
where num < 5
select num;
// Execute the query.
foreach (int i in lowNums)
{
Console.Write(i + " ");
}
}
}
// Output: 4 1 3 2 0
范圍變量
如果數(shù)據(jù)源實(shí)現(xiàn)了 IEnumerable<(Of <(t>)>),則編譯器可以推斷范圍變量的類(lèi)型。例如,如果數(shù)據(jù)源的類(lèi)型為 IEnumerable,則推斷出范圍變量的類(lèi)型為 Customer。僅當(dāng)數(shù)據(jù)源是非泛型 IEnumerable 類(lèi)型(如 ArrayList)時(shí),才必須顯式指定數(shù)據(jù)源類(lèi)型。有關(guān)更多信息,請(qǐng)參見(jiàn) 如何:使用 LINQ 查詢(xún) ArrayList。
在上一個(gè)示例中,num 被推斷為 int 類(lèi)型。由于范圍變量是強(qiáng)類(lèi)型,因此可以對(duì)其調(diào)用方法或者在其他操作中使用它。例如,可以不編寫(xiě) select num,而編寫(xiě) select num.ToString() 使查詢(xún)表達(dá)式返回一個(gè)字符串序列而不是整數(shù)序列。或者,也可以編寫(xiě) select n + 10 使表達(dá)式返回序列 14、11、13、12、10。有關(guān)更多信息,請(qǐng)參見(jiàn) select 子句(C# 參考)。
范圍變量類(lèi)似于 foreach 語(yǔ)句中的迭代變量,只是兩者之間有一個(gè)非常重要的區(qū)別:范圍變量從不實(shí)際存儲(chǔ)來(lái)自數(shù)據(jù)源的數(shù)據(jù)。范圍變量只是提供了語(yǔ)法上的便利,使查詢(xún)能夠描述執(zhí)行查詢(xún)時(shí)將發(fā)生的事情。有關(guān)更多信息,請(qǐng)參見(jiàn) LINQ 查詢(xún)介紹。
復(fù)合 from 子句
在某些情況下,源序列中的每個(gè)元素本身可能是序列,也可能包含序列。例如,數(shù)據(jù)源可能是一個(gè) IEnumerable,其中,序列中的每個(gè) Student 對(duì)象都包含一個(gè)測(cè)驗(yàn)得分列表。若要訪(fǎng)問(wèn)每個(gè) Student 元素中的內(nèi)部列表,可以使用復(fù)合 from 子句。該技術(shù)類(lèi)似于使用嵌套的 foreach 語(yǔ)句?梢韵蛉我 from 子句中添加 where 或 orderby 子句來(lái)篩選結(jié)果。下面的示例演示了一個(gè) Student 對(duì)象序列,其中每個(gè)對(duì)象都包含一個(gè)表示測(cè)驗(yàn)得分的內(nèi)部整數(shù) List。為了訪(fǎng)問(wèn)該內(nèi)部列表,此示例使用了復(fù)合 from 子句。如有必要,可在兩個(gè) from 子句之間再插入子句。
C#
class CompoundFrom
{
// The element type of the data source.
public class Student
{
public string LastName { get; set; }
public ListScores {get; set;}
}
static void Main()
{
// Use a collection initializer to create the data source. Note that
// each element in the list contains an inner sequence of scores.
Liststudents = new List
{
new Student {LastName="Omelchenko", Scores= new List{97, 72, 81, 60}},
new Student {LastName="O'Donnell", Scores= new List{75, 84, 91, 39}},
new Student {LastName="Mortensen", Scores= new List{88, 94, 65, 85}},
new Student {LastName="Garcia", Scores= new List{97, 89, 85, 82}},
new Student {LastName="Beebe", Scores= new List{35, 72, 91, 70}}
};
// Use a compound from to access the inner sequence within each element.
// Note the similarity to a nested foreach statement.
var scoreQuery = from student in students
from score in student.Scores
where score > 90
select new { Last = student.LastName, score };
// Execute the queries.
Console.WriteLine("scoreQuery:");
foreach (var student in scoreQuery)
{
Console.WriteLine("{0} Score: {1}", student.Last, student.score);
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
/*
scoreQuery:
Omelchenko Score: 97
O'Donnell Score: 91
Mortensen Score: 94
Garcia Score: 97
Beebe Score: 91
*/
使用多個(gè) from 子句執(zhí)行聯(lián)接
復(fù)合 from 子句用于訪(fǎng)問(wèn)單個(gè)數(shù)據(jù)源中的內(nèi)部集合。不過(guò),查詢(xún)還可以包含多個(gè)可從獨(dú)立數(shù)據(jù)源生成補(bǔ)充查詢(xún)的 from 子句。使用此技術(shù)可以執(zhí)行某些類(lèi)型的、無(wú)法通過(guò)使用 join 子句執(zhí)行的聯(lián)接操作。
下面的示例演示如何使用兩個(gè) from 子句構(gòu)成兩個(gè)數(shù)據(jù)源的完全交叉聯(lián)接。
C#
class CompoundFrom2
{
static void Main()
{
char[] upperCase = { 'A', 'B', 'C'};
char[] lowerCase = { 'x', 'y', 'z'};
var joinQuery1 =
from upper in upperCase
from lower in lowerCase
select new { upper, lower};
var joinQuery2 =
from lower in lowerCase
where lower != 'x'
from upper in upperCase
select new { lower, upper };
// Execute the queries.
Console.WriteLine("Cross join:");
foreach (var pair in joinQuery1)
{
Console.WriteLine("{0} is matched to {1}", pair.upper, pair.lower);
}
Console.WriteLine("Filtered non-equijoin:");
foreach (var pair in joinQuery2)
{
Console.WriteLine("{0} is matched to {1}", pair.lower, pair.upper);
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
/* Output:
Cross join:
A is matched to x
A is matched to y
A is matched to z
B is matched to x
B is matched to y
B is matched to z
C is matched to x
C is matched to y
C is matched to z
Filtered non-equijoin:
y is matched to A
y is matched to B
y is matched to C
z is matched to A
z is matched to B
z is matched to C
*/
【c#查詢(xún)關(guān)鍵字from 子句的用法】相關(guān)文章:
c#查詢(xún)關(guān)鍵字之group子句的使用09-07
PHP中final關(guān)鍵字用法08-14
Java中synchronized關(guān)鍵字的用法07-23
淺談C#語(yǔ)言的特點(diǎn)11-01
C語(yǔ)言關(guān)鍵字08-31
關(guān)鍵字register分析07-24