Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using Linq to group by object type

Posted on 2011-03-01
1
Medium Priority
?
396 Views
Last Modified: 2012-05-11
I have a list of objects that derive from a common base class i.e. List<BaseObject>. Contained within this list are subclasses ObjectA, ObjectB, ObjectC. There can be many instances of these sub classes contained in List<BaseObject> i.e. ObjectA has 4 instances, ObjectB has 3 instances, and ObjectC has 5 instances. I have the following question, is there a way either using linq or some other method to group each sub  class instance into separate lists for example List<ObjectA>, List<ObjectB> and List<ObjectC> and then execute a common method called getIDs(). This execution ideally would need to happen in parallel.
0
Comment
Question by:Khopkins32
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 35017597
I think this is what you're after, mind you it is only an example--don't get caught up on the classes I chose  : )

I'm not sure what you're after with the "then execute a common method called getIDs()" part, though.
List<Exception> list = new List<Exception>()
{
    new InvalidOperationException(),
    new InvalidProgramException(),
    new InvalidTimeZoneException(),
    new InvalidOperationException(),
    new InvalidProgramException(),
    new InvalidTimeZoneException(),
    new InvalidOperationException(),
    new InvalidProgramException(),
    new InvalidTimeZoneException(),
    new InvalidProgramException(),
    new InvalidTimeZoneException(),
    new InvalidTimeZoneException(),
};

var grouped = from each in list
              group each by each.GetType() into g
              select g.ToList();

List<List<Exception>> categorized = new List<List<Exception>>();

foreach (var group in grouped)
{
    categorized.Add(group);
}

Open in new window

0

Featured Post

Technology Partners: 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

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…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

886 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