當前位置: 首頁>>代碼示例>>C#>>正文


C# Enumerable.GroupBy方法代碼示例

本文整理匯總了C#中System.Linq.Enumerable.GroupBy方法的典型用法代碼示例。如果您正苦於以下問題:C# Enumerable.GroupBy方法的具體用法?C# Enumerable.GroupBy怎麽用?C# Enumerable.GroupBy使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。


在下文中一共展示了Enumerable.GroupBy方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: GroupByEx4

class Pet
{
    public string Name { get; set; }
    public double Age { get; set; }
}

public static void GroupByEx4()
{
    // Create a list of pets.
    List<Pet> petsList =
        new List<Pet>{ new Pet { Name="Barley", Age=8.3 },
                       new Pet { Name="Boots", Age=4.9 },
                       new Pet { Name="Whiskers", Age=1.5 },
                       new Pet { Name="Daisy", Age=4.3 } };

    // Group Pet.Age values by the Math.Floor of the age.
    // Then project an anonymous type from each group
    // that consists of the key, the count of the group's
    // elements, and the minimum and maximum age in the group.
    var query = petsList.GroupBy(
        pet => Math.Floor(pet.Age),
        pet => pet.Age,
        (baseAge, ages) => new
        {
            Key = baseAge,
            Count = ages.Count(),
            Min = ages.Min(),
            Max = ages.Max()
        });

    // Iterate over each anonymous type.
    foreach (var result in query)
    {
        Console.WriteLine("\nAge group: " + result.Key);
        Console.WriteLine("Number of pets in this age group: " + result.Count);
        Console.WriteLine("Minimum age: " + result.Min);
        Console.WriteLine("Maximum age: " + result.Max);
    }

    /*  This code produces the following output:
     
        Age group: 8
        Number of pets in this age group: 1
        Minimum age: 8.3
        Maximum age: 8.3
     
        Age group: 4
        Number of pets in this age group: 2
        Minimum age: 4.3
        Maximum age: 4.9
     
        Age group: 1
        Number of pets in this age group: 1
        Minimum age: 1.5
        Maximum age: 1.5
    */
}
開發者ID:.NET開發者,項目名稱:System.Linq,代碼行數:57,代碼來源:Enumerable.GroupBy

示例2: GroupByEx1

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

// Uses method-based query syntax.
public static void GroupByEx1()
{
    // Create a list of pets.
    List<Pet> pets =
        new List<Pet>{ new Pet { Name="Barley", Age=8 },
                       new Pet { Name="Boots", Age=4 },
                       new Pet { Name="Whiskers", Age=1 },
                       new Pet { Name="Daisy", Age=4 } };

    // Group the pets using Age as the key value 
    // and selecting only the pet's Name for each value.
    IEnumerable<IGrouping<int, string>> query =
        pets.GroupBy(pet => pet.Age, pet => pet.Name);

    // Iterate over each IGrouping in the collection.
    foreach (IGrouping<int, string> petGroup in query)
    {
        // Print the key value of the IGrouping.
        Console.WriteLine(petGroup.Key);
        // Iterate over each value in the 
        // IGrouping and print the value.
        foreach (string name in petGroup)
            Console.WriteLine("  {0}", name);
    }
}
開發者ID:.NET開發者,項目名稱:System.Linq,代碼行數:32,代碼來源:Enumerable.GroupBy

輸出:

8
   Barley
 4
   Boots
   Daisy
 1
   Whiskers

示例3:

IEnumerable<IGrouping<int, string>> query =
    from pet in pets
    group pet.Name by pet.Age;
開發者ID:.NET開發者,項目名稱:System.Linq,代碼行數:3,代碼來源:Enumerable.GroupBy

示例4: GroupByEx3

class Pet
{
    public string Name { get; set; }
    public double Age { get; set; }
}

public static void GroupByEx3()
{
    // Create a list of pets.
    List<Pet> petsList =
        new List<Pet>{ new Pet { Name="Barley", Age=8.3 },
                       new Pet { Name="Boots", Age=4.9 },
                       new Pet { Name="Whiskers", Age=1.5 },
                       new Pet { Name="Daisy", Age=4.3 } };

    // Group Pet objects by the Math.Floor of their age.
    // Then project an anonymous type from each group
    // that consists of the key, the count of the group's
    // elements, and the minimum and maximum age in the group.
    var query = petsList.GroupBy(
        pet => Math.Floor(pet.Age),
        (age, pets) => new
        {
            Key = age,
            Count = pets.Count(),
            Min = pets.Min(pet => pet.Age),
            Max = pets.Max(pet => pet.Age)
        });

    // Iterate over each anonymous type.
    foreach (var result in query)
    {
        Console.WriteLine("\nAge group: " + result.Key);
        Console.WriteLine("Number of pets in this age group: " + result.Count);
        Console.WriteLine("Minimum age: " + result.Min);
        Console.WriteLine("Maximum age: " + result.Max);
    }

    /*  This code produces the following output:
     
        Age group: 8
        Number of pets in this age group: 1
        Minimum age: 8.3
        Maximum age: 8.3
     
        Age group: 4
        Number of pets in this age group: 2
        Minimum age: 4.3
        Maximum age: 4.9
     
        Age group: 1
        Number of pets in this age group: 1
        Minimum age: 1.5
        Maximum age: 1.5
    */
}
開發者ID:.NET開發者,項目名稱:System.Linq,代碼行數:56,代碼來源:Enumerable.GroupBy


注:本文中的System.Linq.Enumerable.GroupBy方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。