into (C# 參考)

更新:2007 年 11 月

into 內容關鍵字可以用於建立暫時識別項,將 groupjoinselect 子句的結果儲存至新的識別項。此識別項本身可以是額外查詢命令的產生器。在 group 或 select 子句中使用時,使用新識別項有時會視為「接續」(Continuation)。

範例

下列範例顯示使用 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(" {0} has {1} elements.", item.FirstLetter, item.Words);
        }

        // Keep the console window open in debug mode
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/* Output:
   a has 2 elements.
   b has 2 elements.
*/

只有當您要在每個群組上執行額外查詢作業時,才需要在 group 子句中使用 into。如需詳細資訊,請參閱 group 子句 (C# 參考)

如需在 join 子句中使用into 的範例,請參閱 join 子句 (C# 參考)

請參閱

概念

LINQ 查詢運算式 (C# 程式設計手冊)

參考

group 子句 (C# 參考)

其他資源

查詢關鍵字 (C# 參考)