Strange .Where() behaviour. Somebody has an explanation?

The only way this could actually be happening is if you were modifying parameter or parameter. Constraint somehow while you're enumerating through transactions So if you're not doing that, look at whether you're actually observing what you think you're observing In principle, this should work fine EDIT: One obvious way you could be confused about your observation is if you didn't check the results of (actually evaluate) the lazy Where enumeration until later on, when parameter had changed. If you put a ToArray on the end to evaluate it immediately, you might find that it "magically" fixes itself.

The only way this could actually be happening is if you were modifying parameter or parameter. Constraint somehow while you're enumerating through transactions. So if you're not doing that, look at whether you're actually observing what you think you're observing.In principle, this should work fine.

EDIT: One obvious way you could be confused about your observation is if you didn't check the results of (actually evaluate) the lazy Where enumeration until later on, when parameter had changed. If you put a ToArray on the end to evaluate it immediately, you might find that it "magically" fixes itself.

I am thinking that this is indeed the case. I will make it a ToList() so it evaluates immediately and I guess my problems are over. – Peterdk Nov 24 '09 at 21:53 I indeed used multiple Where's and multiple parameters all which used a lazy Where.

I now added everywhere a .ToList() and voila, it's fixed. – Peterdk Nov 24 '09 at 21:58.

Undoubtedly a variation of lorgonblog.spaces.live.com/blog/cns!7016... blogs.msdn.com/ericlippert/archive/2009/....

2 I don't see how this would be connected to his issue. Perhaps his problem is a simple misunderstanding of lazy evaluation, but it doesn't seem related to the closure-reference gotcha you cite. – mquander Nov 24 '09 at 21:34.

I just tried this and it works fine: class Program { public class Transaction { public DateTime date { get; set; } } public class Parameter { public string Constraint { get; set; } } public static void Main() { IEnumerable transactions = new List { new Transaction { date = new DateTime(2009, 10, 5) }, new Transaction { date = new DateTime(2009, 11, 3) } }; Parameter parameter = new Parameter { Constraint = "2009-11-01" }; DateTime startDate = DateTime. Parse(parameter. Constraint); // Version 1.

Transactions = transactions. Where(T => T. Date >= startDate); // Version 2.

Transactions = transactions. Where(T => T. Date >= DateTime.

Parse(parameter. Constraint)); } } You must be omitting an important detail. Perhaps you could try reducing your code to the simplest possible example that still reproduces the bug.In doing so, you will probably discover the solution.

If you still can't see the problem you can post the short, compiling, non-working version here and I'm sure someone will spot the problem quickly.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions