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

  • SEARCH
  • Home
  • 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 8187755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:40:09+00:00 2026-06-07T02:40:09+00:00

Suppose I have this LINQ query (on .NET 4.0) : IEnumerable<Service> listService = (from

  • 0

Suppose I have this LINQ query (on .NET 4.0) :

IEnumerable<Service> listService = (from MyObject myObj in new MyObjects()
                                        select myObj.Services);

As you can see, listService is a collection of “collection” (Services is a collection of “Service”, which contains a Title, an ID (what I need), and some other fields.).

What I’d like to do in LINQ is to have, with that query, an IEnumerable<int>, with the Distinct list of ID for each Service for each Services.

Is there a way to do this on LINQ or I need to cycle with some foreach and manage with another arrays?

Example :

my first myObj (so, MyObjects[0]) have got a collection, called Service, which contain single Service. Every Service have got a single id, respectively : "1", "2", "4"

my second myObj (so, MyObjects[1]) have got a collection, called Service, which contain single Service. Every Service have got a single id, respectively : "4", "5", "1", "2"}

What I need is a “single” collection which contains the list of ID from each Service collection from each myObj. This list must be with Distinct value.

  • 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-06-07T02:40:10+00:00Added an answer on June 7, 2026 at 2:40 am

    Since you haven’t shown the content of your classes, I implemented what I could guess from your description. Does this match your objects?

    class Service
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    
    class MyObject
    {
        public IEnumerable<Service> Services { get; set; }
    }
    
    class MyObjects : List<MyObject>
    {
    
    }
    

    If so, then if I stick the following in the constructor for MyObjects:

    class MyObjects : List<MyObject>
    {
        public MyObjects()
        {
            Add(new MyObject 
                { 
                    Services = new List<Service>()
                        {
                            new Service { ID = 1, Name = "foo" },
                            new Service { ID = 2, Name = "bar" },
                        }
                });
            Add(new MyObject
            {
                Services = new List<Service>()
                        {
                            new Service { ID = 3, Name = "baz" },
                            new Service { ID = 4, Name = "foo1" },
                            new Service { ID = 1, Name = "dup 1"}
                        }
            });
        }
    }
    

    I can get the distinct IDs like so:

    var distinctIDs = (from myObj in new MyObjects()
                   from service in myObj.Services
                   select service.ID).Distinct();
    

    The following test program:

    static void Main(string[] args)
    {
        var objects = new MyObjects();
        var distinctIDs = (from myObj in new MyObjects()
                           from service in myObj.Services
                           select service.ID).Distinct();
        var notDistinctIDs = from myObj in new MyObjects()
                             from service in myObj.Services
                             select service.ID;
        foreach (var id in distinctIDs)
        {
            Console.WriteLine("Distinct ID: {0}", id);
        }
        Console.WriteLine("---");
        foreach (var id in notDistinctIDs)
        {
            Console.WriteLine("Not Distinct ID: {0}", id);
        }
    }
    

    prints:

    Distinct ID: 1
    Distinct ID: 2
    Distinct ID: 3
    Distinct ID: 4
    ---
    Not Distinct ID: 1
    Not Distinct ID: 2
    Not Distinct ID: 3
    Not Distinct ID: 4
    Not Distinct ID: 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this: <select id=myselect> <option rel=a>1</option> <option rel=b>2</option> <select> How do I
I have a Linq2Sql query that looks like this: var data = from d
Suppose I have this number list: List<int> = new List<int>(){3,5,8,11,12,13,14,21} Suppose that I want
Suppose I have this (C++ or maybe C) code: vector<int> my_vector; for (int i
Suppose I have this function: void my_test() { A a1 = A_factory_func(); A a2(A_factory_func());
Suppose I have this feature branch foo. Now I want to merge it back
Suppose I have this image: <img src=images/01.jpg border=0 rel=shadow /> Then with jQuery, I
Suppose I have this code: String encoding = UTF-16; String text = [Hello StackOverflow];
Suppose I have this html markup: <div id=wrapper> <pre class=highlight> $(function(){ // hide all
Suppose we have this html content, and we are willing to get Content1, Content2,..

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.