Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6217071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:21:41+00:00 2026-05-24T07:21:41+00:00

I am looking for a elegant solution for the following situation: I have a

  • 0

I am looking for a elegant solution for the following situation:

I have a class that contains a List like

class MyClass{ 
...
 public List<SomeOtherClass> SomeOtherClassList {get; set;}
...
}

A third class called Model holds a List<Myclass> which is the one I am operating on from extern.

Now I would like to extend the Model class with a method that returns all unique SomeOtherClass instances over all MyClass instances.

I know that there is the Union() method and with a foreach loop I could solve this issue easily, which I actually did. However, since I am new to all the C#3+ features I am curious how this could be achieved more elegantly, with or without Linq.

I have found an approach, that seems rather clumsy to me, but it works:

        List<SomeOtherClass> ret = new List<SomeOtherClass>();
        MyClassList.Select(b => b.SomeOtherClasses).ToList().ForEach(l => ret = ret.Union(l).ToList()); 
        return ret;

Note: The b.SomeotherClasses property returns a List<SomeOtherClasses>.

This code is far away from being perfect and some questions arise from the fact that I have to figure out what is good style for working with C#3 and what not. So, I made a little list with thoughts about that snippet, which I would be glad to get a few comments about. Apart from that I’d be glad to hear some comments how to improve this code any further.

  • The temporary list ret would have been part of an approach in C#2 maybe, but is it correct that I should be able to resign this list with using method chaining instead? Or am I missing the point?
  • Is it really required to use the intermediate ToList() method? All I want is to perform a further action each member of a selection.
  • What is the cost of those ToList() operations? Are they good style? Necessary?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:21:42+00:00Added an answer on May 24, 2026 at 7:21 am

    You are looking for SelectMany() + Distinct() :

    List<SomeOtherClass> ret =  MyClassList.SelectMany( x => x.SomeOtherClasses)
                                           .Distinct()
                                           .ToList();
    

    SelectMany() will flatten the “list of lists” into one list, then you can just pick out the distinct entries in this enumeration instead of using union between individual sub-lists.

    In general you will want to avoid side effects with Linq, your original approach is kind of abusing this my modifying ret which is not part of the query.

    ToList() is required since each standard query operator returns a new enumeration and does not modify the existing enumeration, hence you have to convert the final enumeration result back to a list. The cost of ToList() is a full iteration of the enumeration which, in most cases, is negligible. Of course if your class could use an IEnumerable<SomeOtherClass> instead, you do not have to convert to a list at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for an elegant solution to the following problem: I currently have a
I'm looking for an elegant, high performance solution to the following problem. There are
I'm looking for an elegant solution to a formula for the following criteria: Location:
I am looking for an elegant solution for the following problem. Let's assume we
I have been looking for an elegant way to write a function that takes
I'm looking for possible solutions for the following scenario: I have a service that
I'm looking for an elegant Python program that does a BFS traveral of a
I am currently looking for an intelligent best practice solution for the following problem.
The following LINQ statement: public override List<Item> SearchListWithSearchPhrase(string searchPhrase) { List<string> searchTerms = StringHelpers.GetSearchTerms(searchPhrase);
I want to have a database table that keeps data with revision history (like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.