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 7686431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:28:46+00:00 2026-05-31T19:28:46+00:00

I have an object that has 3 separate Dictionaries. The value parameter for each

  • 0

I have an object that has 3 separate Dictionaries. The value parameter for each dictionary implements the same interface. What is the best method to combine the 3 dictionaries into one and perform a single query so the results from the query will be a single IEnumerable?

Here’s a rough idea of I am trying to accomplish. My DataSet object contains 3 dictionaries, each of which should be very small (theoretically some could contain up to 100 elements, but except in the most extreme cases they will be always less than 20 and usually 6 or less).

The purpose of the GetAllId() method will be to retrieve the Id for several private fields in each element of each dictionary and return it as a single IEnumerable. The dictionary value objects all implement IIdQueryable, which defines a single method that will extract all of the required Id‘s in the object.

I have 2 different ideas on how to accomplish what I want, but I am not sure if there is a better way to accomplish this?

public class DataSet
{
    Dictionary<Int32, Foo> dict1;
    Dictionary<CustomKey, Bar> dict2;
    Dictionary<Int32, Boo> dict3;

    public IEnumerable<Int32> GetAllId
    {
        // need to retrieve Id from dict1, dict2, and dict3.
        //  implementation ideas below
    }
}

Option 1

public IEnumerable<Int32> GetAllId
{
    var q1 = dict.Values.SelectMany(g => g.GetId());
    var q2 = dict.Values.SelectMany(g => g.GetId());
    var q3 = dict.Values.SelectMany(g => g.GetId());

    return q1.Concat(q2).Concat(q3);
}

Option 2

public IEnumerable<Int32> GetAllId
{
    var c1 = dict1.Values.Cast<IIdQueryable>();
    var c2 = dict2.Values.Cast<IIdQueryable>();
    var c3 = dict2.Values.Cast<IIdQueryable>();

    var collection = c1.Concat(c2).Concat(c3);

    return collection.SelectMany(g => g.GetId());
}

Method #3

Since each object implements the same interface, is it possible to perform a single LINQ query on all 3 objects without casting?

I personally like Method #1 better as it doesn’t involve casting anything, but I think Method #2 seems to be more readable.

If it is needed, here’s a rough idea of how the interface is implemented

public interface IIdQueryable
{
    IEnumerable<Int32> GetId();
}

public class Foo : IIdQueryable
{
    public IEnumerable<Int32> GetId()
    {
       //returns Id of all elements in this object
    }
}

public class Bar : IGuidQueryable
{
    public IEnumerable<Int32> GetId()
    {
       //returns Id of all elements in this object
    }
}

public class Boo : IGuidQueryable
{
    public IEnumerable<Int32> GetId()
    {
       //returns Id of all elements in this object
    }
}

EDIT:

The question title is the source of what I was hoping could be done (that is do all 3 lookups in a single query without casting). I clarified that above.

  • 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-31T19:28:47+00:00Added an answer on May 31, 2026 at 7:28 pm

    You just need one SelectMany call in the first approach:

    public IEnumerable<Int32> GetAllId()
    {
        return dict1.Values
                    .Select(x => x.GetId())
                    .Concat(dict2.Values.Select( x=> x.GetId()))
                    .Concat(dict3.Values.Select(x => x.GetId()))
                    .SelectMany(x => x);
    }
    

    Personally I wouldn’t duct-tape this together though, there is no performance impact by keeping the queries separate and just returning the concatenation like you did already in the first example – it is more readable to me:

    public IEnumerable<Int32> GetAllId()
    {
        var q1 = dict1.Values.Select(g => g.GetId());
        var q2 = dict2.Values.Select(g => g.GetId());
        var q3 = dict3.Values.Select(g => g.GetId());
    
        return q1.Concat(q2)
                 .Concat(q3)
                 .SelectMany(x => x);
    }
    

    Now this looks pretty close to the second approach already – but no cast needed.

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

Sidebar

Related Questions

I have two tables: object that has object_id column and avalues that have object_id
I have an object that has a list of abstract 'aninamls'. i.e. var animals
I have an object that has a multi-part key and I am struggling to
Lets say I have an object that has stringProp1, stringProp2. I wish to store
I am using an asp:Calander and I have an object that has a beginning
Hey. I have an object that has a string property called BackgroundColor. This string
I have an Asset object that has a property AssignedSoftware, which is a collection.
I have a Task object that has a collection of Label objects ... in
I have a domain object that has been auto generated for me by MyGeneration.
Suppose I have a customer object that has all of the standard customer properties

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.