Solved

Using Linq to group by object type

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

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Aspnet xml 2 36
Windows Service with UDP 2 28
Problem to open Excel file 15 40
Performance  Html.BeginForm vs jQuery 3 13
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now