into 內容關鍵字可用來建立一個暫時的識別碼,以將 組合的結果、連接 或 擇取 子句儲存到新的識別碼中。 此標識元本身可以是其他查詢命令的產生器。 在 group 或 select 子句中使用時,新識別碼的使用有時稱為 接續。
範例
下列範例示範如何使用 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.
*/
只有在您想要在每個群組上執行其他查詢作業時,才需要在 into 子句中使用 group。 如需詳細資訊,請參閱 組別子句。
如需在 into 子句中使用 join 的範例,請參閱 連接子句。