Solved

Using Linq to group by object type

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

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
PrintingFoundIt(Almost!(Damn!)) 1 37
C# LINQ ForEach() question 6 76
VS2010 Build fails to install 14 75
Why use this lambda? 12 60
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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