Solved

How do I join 3 enumerable lists that have been grouped?

Posted on 2009-03-30
8
302 Views
Last Modified: 2013-11-11
I have 3 enumerable lists of the same type - they are employee lists. I want to group each of them by month of birth to get something like:
List 1                                      List 2                                  List3
January   |  8.5%                    January  |  5.5%                January   |  9.0%
February |  9.0%                    February |  11.0%              February |  8.0%

Finally I want to unite these lists so that I can bind the results to a combobox which would look something like this:

January   |   8.5%   |   5.5%   |    9.0%
February  |  9.0%   |   11.0%  |   8.0%


var vGroupList1 = (from res in listSource group res by (res.DateOfBirth.Month).ToString());
 double dCount= (from res in listSource 
                                  select res.EmployeeID).Count();
 foreach (var item in vSelection)
                    {
                        Employee sd = new Employee ();
                        sd.GroupKey = item.Key;
                        sd.GroupCount = (item.Count() / dCount);
                        foreach (Employee subItem in item)
                        {
                            sd.GroupLabel = (String.Format("{0:MMMM}", (subItem.DateOfBirth)));
                            
                        }
                        GroupedList.Add(sd);
                    }

Open in new window

0
Comment
Question by:Jobbietronic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 24022388
Can you post the structure of the employee type?
Are the percentages a field in the employee type?
0
 

Author Comment

by:Jobbietronic
ID: 24023393
The percentages are calculated at runtime using the count of employees for each month of birth divided by the total number of employees in ech list.
The employee type looks something like this   (the 'Group####' bits are there to allow me to manage the rolled up numbers)          
            public int SchoolID { get; set; }
            public int EmployeeID { get; set; }
            public string DeptName { get; set; }
            public DateTime RegDate { get; set; }
            public string Gender { get; set; }            
            public DateTime DateOfBirth { get; set; }
            public string EntryYear { get; set; }
            public string MotherTongue { get; set; }
            public double SENActive { get; set; }
            public string PrimaryNeed { get; set; }
            public double FSMActive { get; set; }
            public string GroupKey { get; set; }
            public string GroupLabel { get; set; }
            public double GroupCount { get; set; }
0
 
LVL 3

Accepted Solution

by:
MikeDotNet555 earned 500 total points
ID: 24023494
That was an interesting question indeed, and I am always looking for new challenges using LINQ. I hope this is what you were looking for. You need to concat your collections befor you group them, then group them. I was not able to concat already grouped collections.

I hope this is what you were looking for.
public class EmployeeTest
    {
        public static void Test()
        {
            employee emp1 = new employee("January", 7.5);
            employee emp2 = new employee("January", 5.6);
            employee emp3 = new employee("June", 4.5);
            List<employee> empList1 = new List<employee>();
            empList1.Add(emp1);
            empList1.Add(emp2);
            empList1.Add(emp3);
 
            employee emp4 = new employee("June", 5.7);
            employee emp5 = new employee("June", 9.5);
            employee emp6 = new employee("January", 6.6);
            List<employee> empList2 = new List<employee>();
            empList2.Add(emp4);
            empList2.Add(emp5);
            empList2.Add(emp6);
 
            employee emp7 = new employee("June", 6.5);
            employee emp8 = new employee("January", 10.5);
            List<employee> empList3 = new List<employee>();
            empList3.Add(emp7);
            empList3.Add(emp8);
 
            var allEmployees = empList1.Concat(empList2).Concat(empList3);
            var grouped = from k in allEmployees group k by k.month into g select g;
 
            foreach (var group in grouped)
            {
                Console.WriteLine("Scores for " + group.Key);
                foreach (var element in group)
                    Console.WriteLine(element.percent);
            }
        }
    }
 
    public class employee
    {
        public string month;
        public double percent;
 
        public employee(string month, double percent)
        {
            this.month = month;
            this.percent = percent;
        }
    }

Open in new window

0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 3

Expert Comment

by:MikeDotNet555
ID: 24023503
forgot:
/*
             * Output:
                Scores for January
                7.5
                5.6
                6.6
                10.5
                Scores for June
                4.5
                5.7
                9.5
                6.5
 
             */
0
 

Author Comment

by:Jobbietronic
ID: 24023803
Hi Mike, thanks for the reply. The concat and 're-group' is very helpfull. Would you reccomend using a foreach loop to add each of the 12 list items (i.e. months) for each of the three lists into the new employee classes rather than adding them declaratively?
0
 
LVL 3

Expert Comment

by:MikeDotNet555
ID: 24023990
yes you could use a foreach loop or the concat operator, performan-wise I think the concat will be better (I am far from being sure tho).

Btw in the code above I do not "re-group" I just group once after concatenating (spelling?) all the non-grouped lists.
0
 

Author Comment

by:Jobbietronic
ID: 24026589
Cheers Mike,
by regroup I meant grouping the concatenated list after I had done the intial grouping to get the percentages.
Thanks again for your help.
J
0
 

Author Closing Comment

by:Jobbietronic
ID: 31564328
Thanks - really helpful and I appreciate the extra information.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSIS Start 2 37
I need to make my existing Javascript Menu conditional within my ASP.Net/C# Application 32 35
Header Font Size in Grid View 6 23
Return array 3 19
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question