C#, Linq Groupe Par sur plusieurs colonnes [en double]

cette question a déjà une réponse ici:

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, J'aimerais utiliser LINQ pour créer une liste à partir de la liste, groupée par l'école, ami et propriétés FavoriteColor. Est-ce possible avec LINQ?

s'il vous Plaît ignorer les propriétés, le code a été écrit juste pour aider avec la question.

221
demandé sur Kasy 2011-03-08 14:30:50

2 réponses

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(),
    };
346
répondu Enigmativity 2015-11-28 01:00:39

donne 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 à:

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 D'essai:

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
175
répondu Jamiec 2014-08-12 13:10:29