376 votes

C # Linq Group By sur plusieurs colonnes

 public class ConsolidatedChild
{
    public string School { get; set; }
    public string Friend { get; set; }
    public string FavoriteColor { get; set; }
    public List<Child> Children { get; set; }
}

public class Child
{
    public string School { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string Friend { get; set; }
    public string Mother { get; set; }
    public string FavoriteColor { get; set; }
}
 

Étant donné les deux classes ci-dessus, je voudrais utiliser LINQ pour créer une liste à partir de la liste, regroupée par les propriétés School, Friend et FavoriteColor. Est-ce possible avec LINQ?

Veuillez ignorer les propriétés, le code a été écrit juste pour vous aider avec la question.

620voto

Enigmativity Points 26345

Facile:

 var consolidatedChildren =
    from c in children
    group c by new
    {
        c.School,
        c.Friend,
        c.FavoriteColor,
    } into gcs
    select new ConsolidatedChild()
    {
        School = gcs.Key.School,
        Friend = gcs.Key.Friend,
        FavoriteColor = gcs.Key.FavoriteColor,
        Children = gcs.ToList(),
    };
 

236voto

Jamiec Points 35773

Donné une liste:

 var list = new List<Child>()
                {
                    new Child()
                        {School = "School1", FavoriteColor = "blue", Friend = "Bob", Name = "John"},
                    new Child()
                        {School = "School2", FavoriteColor = "blue", Friend = "Bob", Name = "Pete"},
                    new Child()
                        {School = "School1", FavoriteColor = "blue", Friend = "Bob", Name = "Fred"},
                    new Child()
                        {School = "School2", FavoriteColor = "blue", Friend = "Fred", Name = "Bob"},
                };
 

La requête ressemblerait à ceci:

 var newList = list.GroupBy(x => new {x.School, x.Friend, x.FavoriteColor})
                    .Select(y => new ConsolidatedChild()
                                        {
                                            FavoriteColor = y.Key.FavoriteColor,
                                            Friend = y.Key.Friend,
                                            School = y.Key.School,
                                            Children = y.ToList()
                                        }
                    );
 

Code de test:

 foreach(var item in newList)
{
    Console.WriteLine("School: {0} FavouriteColor: {1} Friend: {2}", item.School,item.FavoriteColor,item.Friend);
    foreach(var child in item.Children)
    {
        Console.WriteLine("\t Name: {0}", child.Name);
    }
}
 

Résultat:

 School: School1 FavouriteColor: blue Friend: Bob
         Name: John
         Name: Fred
School: School2 FavouriteColor: blue Friend: Bob
         Name: Pete
School: School2 FavouriteColor: blue Friend: Fred
         Name: Bob
 

1voto

Kamyar Points 11012

Oui c'est possible. Jetez un oeil à l'échantillon fourni dans ce post.

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X