English 中文(简体)
Strange .Where() behaviour. Somebody has an explanation?
原标题:

Original

I don t get why the Where() clause doesn t give me the right results in the last example.
It isn t any different is it? Why does C# behaves differently?

transactions = IEnumerable<Transaction> //pseudocode 

//This works: It gives me the transaction I need.
DateTime startDate = DateTime.Parse(parameter.Constraint); 
transactions = transactions.Where(T => T.date >= startDate);  

//This doesn t work... No actual code changed, only the way of writing it...
//I get 0 results.
 transactions = transactions.Where(T => T.date >= DateTime.Parse(parameter.Constraint));

Edit

Ok, it is indeed relevant to mention that transactions is loaded using Entity Framework.

transactions = this.db.Include("blablabla").OrderByDescending(T => T.date);

Maybe that s why it s doing weird? Because of the way Entity Linq works?

最佳回答

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 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<Transaction> transactions = new List<Transaction> {
            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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签