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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:34:54+00:00 2026-06-18T10:34:54+00:00

Situation: I have a List<IQueryable<MyDataStructure>> . I want to run a single linq query

  • 0

Situation: I have a List<IQueryable<MyDataStructure>>. I want to run a single linq query on each of them, in parallel, and then join the results.

Question: How to create a linq query which I can pass as a parameter?

Example code:

Here’s some simplified code. First, I have the collection of IQueryable<string>:

    public List<IQueryable<string>> GetQueries()
    {
        var set1 = (new List<string> { "hello", "hey" }).AsQueryable();
        var set2 = (new List<string> { "cat", "dog", "house" }).AsQueryable();
        var set3 = (new List<string> { "cat", "dog", "house" }).AsQueryable();
        var set4 = (new List<string> { "hello", "hey" }).AsQueryable();

        var sets = new List<IQueryable<string>> { set1, set2, set3, set4 };

        return sets;
    }

I would like to find all the words which start with letter ‘h’. With a single IQueryable<string> this is easy:

query.Where(x => x.StartsWith("h")).ToList()

But I want to run the same query against all the IQueryable<string> objects in parallel and then combine the results. Here’s one way to do it:

        var result = new ConcurrentBag<string>();
        Parallel.ForEach(queries, query =>
        {
            var partOfResult = query.Where(x => x.StartsWith("h")).ToList();

            foreach (var word in partOfResult)
            {
                result.Add(word);
            }
        });

        Console.WriteLine(result.Count);

But I want this to be a more generic solution. So that I could define the linq operation separately and pass it as a parameter to a method. Something like this:

        var query = Where(x => x.FirstName.StartsWith("d") && x.IsRemoved == false)
            .Select(x => x.FirstName)
            .OrderBy(x => x.FirstName);

        var queries = GetQueries();

        var result = Run(queries, query);

But I’m at loss on how to do this. Any ideas?

  • 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-18T10:34:55+00:00Added an answer on June 18, 2026 at 10:34 am

    So the first thing that you wanted was a way of taking a sequence of queries, executing all of them, and then getting the flattened list of results. That’s simple enough:

    public static IEnumerable<T> Foo<T>(IEnumerable<IQueryable<T>> queries)
    {
        return queries.AsParallel()
                .Select(query => query.ToList())
                .SelectMany(results => results);
    }
    

    For each query we execute it (call ToList on it) and it’s done in parallel, thanks to AsParallel, and then the results are flattened into a single sequence through SelectMany.

    The other thing that you wanted to do was to add a number of query operations to each query in a sequence of queries. This doesn’t need to be parallelized (thanks to deferred execution, the calls to Where, OrderBy, etc. take almost no time) and can just be done through Select:

    var queries = GetQueries().Select(query =>
        query.Where(x => x.FirstName.StartsWith("d")
            && !x.IsRemoved)
        .Select(x => x.FirstName)
        .OrderBy(x => x.FirstName));
    
    var results = Foo(queries);
    

    Personally I don’t really see a need to combine these two methods. You can make a method that does both, but they’re really rather separate concepts so I don’t see a need for it. If you do want them combined though, here it is:

    public static IEnumerable<TResult> Bar<TSource, TResult>(
        IEnumerable<IQueryable<TSource>> queries,
        Func<IQueryable<TSource>, IQueryable<TResult>> selector)
    {
    
        return queries.Select(selector)
            .AsParallel()
            .Select(query => query.ToList())
            .SelectMany(results => results);
    }
    

    Feel free to make either Foo or Bar extension methods if you want. Also, you really better rename them to something better if you’re going to use them.

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

Sidebar

Related Questions

I have a situation where i want to return List<> from this function public
I have this situation where I want to display a list of Administration objects
I came upon strange situation. I have a List and each list item consists
I have a situation in C# where I have a list of simple types.
Here is my situation: I have one table that contains a list of drugs
I have a situation where the second select list option is generated from the
I have a situation where I need to dynamically build up a list of
I have a situation like this. I have a list of urls in a
For a game I'm making I have a situation where I have a list
I have list on android. Each time user slide screen left or right I

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.