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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:51:14+00:00 2026-05-16T13:51:14+00:00

I cannot figure this out. The workflow of passing IEnumerable<T> (T is some my

  • 0

I cannot figure this out. The workflow of passing IEnumerable<T> (T is some my class, but it is not relevant here) basically looks like this:

var a = GetEntireCollection(); // so I get IEnumerable<T>
...
var b = a.Where(condition1);
...
var c = b.Where(condition2);
...

So I filter out more and more items from the collection, finally I call:

if (z.IsEmpty())
    throw new Exception();
Foo(z);

and Foo is

public void Foo(IEnumerable<T> p)
{
    pool = p.OrderByDescending(it => it.MyProperty).ToList();

    if (pool.IsEmpty())
        throw new Exception(pool.Count().ToString() + ", " + p.Count().ToString());
    ...

All I do, is order the collection.

Now, my program crashes with exception — it says that p has Count = 1, and pool has Count = 0. What’s more when I point out p and require the results (I run program using Visual Studio) it says, the collection yielded with no results (or somethig similar, not verbatim quote).

Questions:

  1. how can non-empty collection become empty just by reordering?
  2. how can collection Count can be > 0, when there are no items in it?

I am asking because I would like to know how to avoid this situation, but honestly, when I am looking at the code it seems 100% legit for me.

Technical background:

  • it is pure C# code, no asm inlines, or anything like this
  • no threads
  • no external libraries, except for Where (which comes from Linq) this is all my code

Edits

Edit 1

public static bool IsEmpty<T>(this IEnumerable<T> coll)
{
    var iter = coll.GetEnumerator();
    return !iter.MoveNext();
}

Edit 2

Just before I call Foo(z) I check if the z is empty, so the code looks like this:

if (z.IsEmpty())
    throw new Exception();
Foo(z);

SOLVED

As Jon suggested (C# sharpshooting I would say) one of the conditions was time dependent. So when the collection evaluation was forced the condition changed and I get in fact another collection.

  • 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-16T13:51:14+00:00Added an answer on May 16, 2026 at 1:51 pm

    One possibility is that evaluating the conditions twice is effectively changing the results.

    Each time you iterate through p, it will re-evaluate the data, including the Where clauses. So you’re doing that once to populate pool, and then again to print out p.Count().

    Without any more information about what GetEntireCollection does or what the Where conditions are, it’s hard to say what’s going on… but that’s one possible explanation. If you can post a short but complete program demonstrating the problem, that would help a lot.

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

Sidebar

Related Questions

I just cannot figure this out, it looks really simple but I'm relatively new
I cannot figure this one out, but I cannot seem to get android to
I'm feeling silly that I cannot figure this out, but it's really starting to
I have tried my best to solve this but i cannot figure this out.
Probably a simple question, but I cannot figure this out. In this plugin: https://github.com/brianmario/jquery-infinite-scroll
For sure this question is very easy, but I just cannot figure this out.
I cannot figure this out. I need to have an abstract template base class,
I am learning to use polymorphism in C#, but cannot figure out this one.
I cannot figure this out from what I have read on Google, but I
I cannot figure this out.. Looks like I'm missing something simple? What do 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.