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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:01:49+00:00 2026-05-23T06:01:49+00:00

I have a method that needs to process an incoming sequence of commands and

  • 0

I have a method that needs to process an incoming sequence of commands and split the results into different buckets depending on some properties of the result. For example:

class Pets
{
    public IEnumerable<Cat> Cats { get; set; }
    public IEnumerable<Dog> Dogs { get; set; }
}

Pets GetPets(IEnumerable<PetRequest> requests) { ... }

The underlying model is perfectly capable of handling the entire sequence of PetRequest elements at once, and also the PetRequest is mostly generic information like an ID, so it makes no sense to try to split the requests at the input. But the provider doesn’t actually give back Cat and Dog instances, just a generic data structure:

class PetProvider
{
    IEnumerable<PetData> GetPets(IEnumerable<PetRequest> requests)
    {
        return HandleAllRequests(requests);
    }
}

I’ve named the response type PetData instead of Pet to clearly indicate that it is not a superclass of Cat or Dog – in other words, conversion to Cat or Dog is a mapping process. The other thing to keep in mind is that HandleAllRequests is expensive, e.g. a database query, so I really don’t want to repeat it, and I would prefer to avoid caching the results in memory using ToArray() or the like, because there might be thousands or millions of results (I have a lot of pets).

So far I’ve been able to throw together this clumsy hack:

Pets GetPets(IEnumerable<PetRequest> requests)
{
    var data = petProvider.GetPets(requests);
    var dataGroups = 
        from d in data
        group d by d.Sound into g
        select new { Sound = g.Key, PetData = g };
    IEnumerable<Cat> cats = null;
    IEnumerable<Dog> dogs = null;
    foreach (var g in dataGroups)
        if (g.Sound == "Bark")
            dogs = g.PetData.Select(d => ConvertDog(d));
        else if (g.Sound == "Meow")
            cats = g.PetData.Select(d => ConvertCat(d));
    return new Pets { Cats = cats, Dogs = dogs };
}

This technically works, in the sense that it doesn’t cause the PetData results to be enumerated twice, but it has two major problems:

  1. It looks like a giant pimple on the code; it smacks of the awful imperative style we always used to have to employ in the pre-LINQ framework 2.0.

  2. It ends up being a thoroughly pointless exercise, because the GroupBy method is just caching all those results in memory, which means I’m really no better off than if I’d just been lazy and done a ToList() in the first place and attached a few predicates.

So to restate the question:

Is it possible to split a single deferred IEnumerable<T> instance into two IEnumerable<?> instances, without performing any eager evaluations, caching results in memory, or having to re-evaluate the original IEnumerable<T> a second time?

Basically, this would be the reverse of a Concat operation. The fact that there isn’t already one in the .NET framework is a strong indication that this may not even be possible, but I thought it wouldn’t hurt to ask anyway.

P.S. Please don’t tell me to create a Pet superclass and just return an IEnumerable<Pet>. I used Cat and Dog as fun examples, but in reality the result types are more like Item and Error – they are both derived from the same generic data but otherwise have nothing in common at all.

  • 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-23T06:01:50+00:00Added an answer on May 23, 2026 at 6:01 am

    Fundamentally, no. Imagine if it were possible. Then consider what happens if I do:

    foreach (Cat cat in pets.Cats)
    {
        ...
    }
    
    foreach (Dog dog in pets.Dogs)
    {
        ...
    }
    

    That needs to handle all the cats first, and then all the dogs… so what could happen with the original sequence if the first element is a Dog? It either has to cache it or skip it – it can’t return it, because we’re still asking for Cats.

    You could implement something which only caches as much as it needs to, but that’s likely to be the whole of one sequence, as typical usage is to completely evaluate one sequence or the other.

    If at all possible, you really just want to handle pets (whether cats or dogs) as you fetch them. Would it be feasible to provide an Action<Cat> and an Action<Pet> and execute the right handler for each item?

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

Sidebar

Related Questions

I have a method that needs to accept an array of country names, and
I have a C# class which needs to process a sequence of items (
Let's say I have a subroutine/method that a user can call to test some
I have a complex save process (a cyclical reference that needs to be resolved
I'm writing some code that needs to process an arbitrary number of lists of
I have a JavaScript method that I need to run on one of my
I need a Regex that will match a java method declaration. I have come
I have a interface that defines some methods with attributes. These attributes need to
I have class method that returns a list of employees that I can iterate
I have a method that where I want to redirect the user back to

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.