none
Lambda Groupby 2 or more properties. Why is this not working RRS feed

  • Question

  • class Person {
    internal int PersonID;
    internal string car ;
    internal string FirstName;
    internal string LastName;
    internal string CarSeries;
    }

    //Program snippet below not working correctly

    Person[] persons = newPerson[3];
    persons[0] = newPerson { PersonID = 2, car = "Ferrari", FirstName="Martin", LastName="James", CarSeries="Series A" };
    persons[0] = newPerson { PersonID = 2, car = "Ferrari", FirstName="Martin", LastName="James" , CarSeries="Series B" };
    persons[0] = newPerson { PersonID = 2, car = "Ferrari", FirstName="Martin", LastName="James" , CarSeries="Series C" };
    persons[1] = newPerson { PersonID = 1, car = "BMW", FirstName="Joanne", LastName="James" , CarSeries="Series 1" };
    persons[1] = newPerson { PersonID = 1, car = "BMW", FirstName="Joanne", LastName="James" , CarSeries="Series 3" };
    persons[2] = newPerson { PersonID = 2, car = "Audi", FirstName="Martin", LastName="James" , CarSeries="A3" };
    persons[2] = newPerson { PersonID = 1, car = "Porche", FirstName="Joanne", LastName="James" , CarSeries="Series Carrera" };
    persons[2] = newPerson { PersonID = 3, car = "Lexus", FirstName="Leon", LastName="James" , CarSeries="S Class"};
    Console.Out.WriteLine("--------------------------------------------");
    varresults5 = persons.GroupBy(p=> new {FirstName=p.FirstName,LastName=p.LastName, Car=p.car, CarSeries= p.CarSeries}).
    Select(grp => new { Name = grp.Key.FirstName+" "+grp.Key.LastName, Car = grp.Key.Car, CarSeries=grp.Key.CarSeries}).ToList();
    foreach(vargpin results5){
    Console.Out.WriteLine(gp.Name+", "+gp.Car+", "+gp.CarSeries);
    }

    noviceDeveloper


    • Edited by DeltaXXX Friday, March 1, 2019 9:35 PM including snippets
    Friday, March 1, 2019 9:33 PM

All replies

  • Maybe you should write ‘Person [ ] persons = new Person[8]’, then use different indices: person[0], person[1], …, person[7]. Currently you overwrite and lose some data. (Or use a list instead of array).

    If still does not work, then show the expected results.

    • Edited by Viorel_MVP Saturday, March 2, 2019 8:12 AM
    Saturday, March 2, 2019 8:09 AM
  • You see this gives me:

    Martin James, Ferrari, Series C
    Joanne James, BMW, Series 3
    Leon James, Lexus, S Class

    Where is the group

    Martin James, Ferrari, Series B

    and others are missing, as they should be in group of themselves, that I am grouping by all 3 properties. I could have initialized persons array in different ways, which has nothing to do with the results set. I think the results should consist of more than is showing. 


    noviceDeveloper

    Saturday, March 2, 2019 8:44 AM
  • Sorry, it works actually this was code I copied and pasted off from some resource. Didn't realise I was overwriting some of the references to Person. My apologies. It works as expected.

    The code should be:

    Person[] persons = new Person[]{ new Person { PersonID = 2, car = "Ferrari", FirstName="Martin", LastName="James", CarSeries="Series A" },
    new Person { PersonID = 2, car = "Ferrari", FirstName="Martin", LastName="James" , CarSeries="Series B" },
    new Person { PersonID = 2, car = "Ferrari", FirstName="Martin", LastName="James" , CarSeries="Series C" },
    new Person { PersonID = 1, car = "BMW", FirstName="Joanne", LastName="James" , CarSeries="Series 1" },
    new Person { PersonID = 1, car = "BMW", FirstName="Joanne", LastName="James" , CarSeries="Series 3" },
    new Person { PersonID = 2, car = "Audi", FirstName="Martin", LastName="James" , CarSeries="A3" },
    new Person { PersonID = 1, car = "Porche", FirstName="Joanne", LastName="James" , CarSeries="Series Carrera" },
    new Person { PersonID = 3, car = "Lexus", FirstName="Leon", LastName="James" , CarSeries="S Class"}
    };
    Console.Out.WriteLine("--------------------------------------------");
    var results7 = from p in persons
    group p by new {p.car, p.FirstName, p.LastName, p.CarSeries} into gp
    from g in gp
    select new {Name=g.FirstName+" "+g.LastName, Car=g.car, CarSeries = g.CarSeries, Count=gp.Count()};
    foreach(var p in results7){
    Console.Out.WriteLine(p.Name+", "+p.Car+", "+p.CarSeries+" Count "+p.Count);
    }


    noviceDeveloper

    Saturday, March 2, 2019 12:53 PM
  • Hi DeltaXXX,

    Thanks for your sharing.

    If your question has been solved, please mark the useful reply as answer to close the thread. This will make answer searching easier in the forum and be beneficial to community members.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 4, 2019 2:03 AM
    Moderator