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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:15:24+00:00 2026-06-12T10:15:24+00:00

The following code is intended to reduce redundant code by allowing you to generically

  • 0

The following code is intended to reduce redundant code by allowing you to generically invoke the same selector on a set of “getters” (data retrieval clients) and have all of the results combined. The final result set is awaitable, allowing the system to not block while waiting for all of the selectors to complete.

public async Task<T> GetBulkAsync<T>(Func<IGetter, Task<T>> selector) where T : IEnumerable<T>
{
    Task<T>[] tasks = new Task<T>[this.getters.Count];
    for (int i = 0; i < this.getters.Count; i++)
    {
        tasks[i] = selector(this.getters[i]);
    }

    T[] results = await Task.WhenAll(tasks);

    return results.SelectMany<T, T>(r => r);
}

However, a problem comes up in my design here. There is one case where I simply want to use a selector that retrieves a single IData and then there’s times where a selector will retrieve an IEnumerable<IData> or maybe a List<IData> or something entirely different.

I have thought about using reflection to handle special cases with generic collections, but that’s messy and requires additional work for every type of collection I want to handle.

The main reason I care about IEnumerable<IData> (or really, IEnumerable<T>) is because I want to use Linq’s SelectMany in order to flatten out all of the results in the big result set. If each selector is coming back with an IEnumerable<T>, it needs to be reduced into one IEnumerable<T>, not an IEnumerable<IEnumerable<T>>.

Is there a good approach to solving this issue other than writing several different generic methods with constraints on collection types and without using reflection?

  • 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-12T10:15:26+00:00Added an answer on June 12, 2026 at 10:15 am

    Instead of relying on one type parameter, use two and provide a transformation/projection function at the end, like so:

    public async Task<TResult> GetBulkAsync<T, TResult>(
        Func<IGetter, Task<T>> selector,
        Func<IEnumerable<T>, TResult> aggregateSelector) 
            where T : IEnumerable<T>
    {
        Task<T>[] tasks = new Task<T>[this.getters.Count];
        for (int i = 0; i < this.getters.Count; i++)
        {
            tasks[i] = selector(this.getters[i]);
        }
    
        T[] results = await Task.WhenAll(tasks);
    
        return results.Select<T, TResult>(aggregateSelector);
    }
    

    The aggregateSelector would basically transform each IEnumerable<T> into a new TResult instance and you’d return an IEnumerable<TResult>.

    However, I recommend against this, as you’re relying on composition to occur in the function, when it really doesn’t have to.

    Instead, I think you should return a sequence of sequences, like so:

    public async Task<IEnumerable<IEnumerable<T>>> GetBulkAsync<T>(
        Func<IGetter, Task<T>> selector) 
            where T : IEnumerable<T>
    {
        Task<T>[] tasks = new Task<T>[this.getters.Count];
        for (int i = 0; i < this.getters.Count; i++)
        {
            tasks[i] = selector(this.getters[i]);
        }
    
        T[] results = await Task.WhenAll(tasks);
    
        return results.Select(t => t.Result):
    }
    

    This way, you can compose the results any way you want without having to change this method.

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

Sidebar

Related Questions

The following code should exhibit intended functionality: <head> <style> a:active { color:teal; } a:hover
consider the following code, which represents an attempt to implement partial matching. the intended
Following code iterates through many data-rows, calcs some score per row and then sorts
The following code is intended to retrieve a file via FTP. However, I'm getting
Following code does destroy records as intended, but the callback is inherited from one
I'm not sure why the following code is not working as intended: litMessage.Text =
The following code #include stdafx.h #include <string> #include <set> #include <boost/algorithm/string/trim.hpp> int _tmain(int argc,
I have the following code which is intended to check availability of links from
I have written the following code to set the cursor of a Gtk::Window from
The following code is intended to add a newly deployed EC2 instance in a

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.