Solved

Using Linq to group by object type

Posted on 2011-03-01
1
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

627 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