locked
How to Group Data Using Linq and Nested Containers

    Question

  • In a Windows 8 Store app I am trying to group people by the date of their appointment using Linq. The following code does not compile.

         private List<GroupedPersons> MakeGroupedPatients(DateTime[] dates, IEnumerable<Person> persons)
         {           

                if (persons == null)
                    return null;
                else
                {
                    var query = from date in dates
                                join person in persons
                                on date.Date equals (from appointment in person.Appointments select appointment.StartDateTime.Date)
                                into j1

                                from j2 in j1.DefaultIfEmpty()

                                group j2 by date.Date
                                    into g

                                    orderby g.Key ascending
                                    select new GroupedPersons
                                    {
                                        GroupStartDate = g.Key.ToString("g"),
                                        Patients = new ObservableCollection<Person>(g.ToList())
                                    };

                    return new List<GroupedPersons>(query.ToList());
               
                }
         }

    I'm passing in an array of dates and a listing of persons, where each person has a List of appointments, and each appointment has a StartDateTime of type DateTime. What's wrong here? Thanks.

    Sunday, November 02, 2014 10:23 PM

Answers

  • Hi IMOsiris,

    Per my understanding, you are writing an incorrect LINQ. When you join two object arrays together, you should indicate two properties in dates and persons array. So you should find a property from persons array and let it stand behind equals keyword. You are not allowed to put an expression to instead. LINQ expression should be like the following.

    var query = from date in dates
                                join person in persons
                                on date.Date equals person.Date
                                into j1
                                from j2 in j1.DefaultIfEmpty()
                                group j2 by date.Date
                                    into g
                                    orderby g.Key ascending
                                    select new GroupedPersons
                                    {
                                        GroupStartDate = g.Key.ToString("g"),
                                        Patients = new ObservableCollection<Person>(g.ToList())
                                    };
    

    According to your LINQ expression, I assume you want to check if the value of persions. Appointment equals to dates array. Just use Foreach keyword before LINQ expression to filter the array first, and then using LINQ to group by the result.

    If you need more help, please post more information for reproduction.

    If I misunderstand you, please feel free to let me know.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Monday, November 03, 2014 9:39 AM
    Moderator