LINQ group by enum and map to another enum

Dear Experts:
I need to group a list of customers (List<Customer>) where Customer contains this enum

public enum CustomerType
{
  type1,
  type2,
  type3,
  type4,
  type5
}

Open in new window


to a new enum
public enum NewCustomerType
{
  newtype1,  //map to type1
  newtype2,  //map to type2 and 3
  newtype3,  //map to type4 and 5 
}

Open in new window


Note: My initial code to map to the first enum:
var sortedDetails = _customers.GroupBy(x => x.CustomerType).ToDictionary(t => t.Key, t => t.ToList());
Your answer should return a dictionary mapped to the new enum.

Thanks
LVL 36
Miguel OzSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
What is the point of this code? I get that you want to remap the customer type, but what is the purpose of the GroupBy?
0
Miguel OzSoftware EngineerAuthor Commented:
If you are refering to the initial code sample is just to show you an initial attempt,. My expectations is that your answer should provide me with a dictionary sorted by the new enum.
0
käµfm³d 👽Commented:
I would write a mapping function to do the conversion:

static NewCustomerType Map(CustomerType type)
{
    switch (type)
    {
        case CustomerType.type2:
        case CustomerType.type3:
            return NewCustomerType.newtype2;
        case CustomerType.type4:
        case CustomerType.type5:
            return NewCustomerType.newtype3;
        default:
            return NewCustomerType.newtype1;
    }
}

Open in new window


...and then you can use that function within the GroupBy:

var sorted = customers.GroupBy(c => Map(c.CustomerType))
                      .ToDictionary(t => t.Key, t => t.ToList());

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Miguel OzSoftware EngineerAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.