使用into上下文關鍵字建立一個暫存識別碼,儲存 、 join或 select 子句的group結果。 此識別碼可作為額外查詢指令的產生器。 當你在 or select 子句中使用新識別碼group時,有時稱為延續。
C# 語言參考資料記錄了 C# 語言最新版本。 同時也包含即將推出語言版本公開預覽功能的初步文件。
文件中標示了語言最近三個版本或目前公開預覽版中首次引入的任何功能。
小提示
欲查詢某功能何時首次在 C# 中引入,請參閱 C# 語言版本歷史的條目。
以下範例展示了如何使用關鍵字 into 建立名為 fruitGroup的臨時識別碼,其推斷型態為 IGrouping。 透過使用此識別碼,你可以在每個群組呼叫該 Count 方法,並只選擇包含兩個或以上單字的群組。
class IntoSample1
{
static void Main()
{
// Create a data source.
string[] words = ["apples", "blueberries", "oranges", "bananas", "apricots"];
// Create the query.
var wordGroups1 =
from w in words
group w by w[0] into fruitGroup
where fruitGroup.Count() >= 2
select new { FirstLetter = fruitGroup.Key, Words = fruitGroup.Count() };
// Execute the query. Note that we only iterate over the groups,
// not the items in each group
foreach (var item in wordGroups1)
{
Console.WriteLine($" {item.FirstLetter} has {item.Words} elements.");
}
}
}
/* Output:
a has 2 elements.
b has 2 elements.
*/
只有當你想對每個群組執行額外查詢操作時,才需要在子group句中使用into。 如需詳細資訊,請參閱 組別子句。
關於在子join句中使用into的例子,請參見 join 子句。