4

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.

8
  • do you have threads in your program? Sep 2, 2010 at 5:46
  • I just wanted to add this piece :-) -- no. Sep 2, 2010 at 5:54
  • That IsEmpty() is a bit iffy to me: no disposal of the IEnumerator<T>, plus it's basically just duplicating !Any().
    – Dan Tao
    Sep 2, 2010 at 6:29
  • 1
    @macias: In most cases it ends up not being a big deal. I'm just pointing out that your IsEmpty method accepts any IEnumerable<T> implementation, so it should dispose of the object passed to it, as the contract of IEnumerable<T> dictates. It's not really about clashes, necessarily. You could have a type that connects to a data feed over a network connection, for example. Just a bit of general advice -- not the most important thing in the world.
    – Dan Tao
    Sep 2, 2010 at 6:59
  • 1
    @macias: Sorry, by "the object passed to it," I meant to say, "the enumerator provided by the object passed to it" (i.e., whatever's returned by GetEnumerator).
    – Dan Tao
    Sep 2, 2010 at 7:06

3 Answers 3

3

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.

4
  • Thank you, it could be a cause, when looking I don't see any time dependant conditions, but of course I can miss something. I will try to track it (or simplify the code) and will post update ASAP. Sep 2, 2010 at 6:35
  • I found out I already added check for being empty before I call Foo. Even with deferred exection should this condition hold true (once executed) in all subsequent calls? Sep 2, 2010 at 6:45
  • @macias: Nope - because unless z is something concrete and non-deferred (like a List<T>) its results can change every time you iterate over it. Note that it doesn't have to be time-dependent... if any of your conditions have side effects, that could cause the same kind of problem.
    – Jon Skeet
    Sep 2, 2010 at 6:59
  • Found it :-( Sad face because it is embarrasing for me, however on the other side it was very valuable lesson for me -- despite I know about late execution I was bite so easily ;-). Many thanks (I cannot upvote +10 for the help though :-D)!!! Sep 2, 2010 at 7:24
1

It's empty because of Deferred Execution, most query operators execute not when constructed, but when enumerated (in other words, when MoveNext is called on its enumerator).

So if you change it to :

var c = b.Where(condition2).ToList(); 

And then call Foo(c) it will work.

9
  • 2
    I don't think so -- the Count call should cause p to be evaluated.
    – Dan Tao
    Sep 2, 2010 at 6:06
  • While this may end up being explained by repeated evaluation (which is in turn due to deferred execution) you haven't really explained why p.Count() would be 1 but pool.Count() would be 0. We don't really have enough information at the moment.
    – Jon Skeet
    Sep 2, 2010 at 6:08
  • @Dan: Count call is made in the Exception, before that I don't see any count call.
    – ace
    Sep 2, 2010 at 6:12
  • @ace: So that forces it to be evaluated once, and prior to that it will be evaluated due to the OrderByDescending(...).ToList() call.
    – Jon Skeet
    Sep 2, 2010 at 6:17
  • @ace: But the exception is thrown on pool.IsEmpty(), which shouldn't be subject to deferred execution since pool is a List<T>. It seems the main question is: why would pool.Count() and p.Count() return disparate values? Personally I don't think deferred execution can answer this.
    – Dan Tao
    Sep 2, 2010 at 6:26
0

I think the code you show will call GetEntireCollection multiple times.

So maybe this method is returning different results on successive invocations.

1
  • No, it's not going to call GetEntireCollection multiple times. The source code within GetEntireCollection will execute multiple times if it's an iterator block, but the method itself will only be called once.
    – Jon Skeet
    Sep 2, 2010 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.