Solved

Using Linq to group by object type

Posted on 2011-03-01
1
389 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 500 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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