none
Composable query, 2 wheres with same field does not work ??? RRS feed

  • Question

  • Here is a code snippet and generated SQL.  I am trying to add 2 where clauses that reference the same field, but the 2 sql parameters always are the same in the generated SQL !!!!  (in this case, 9:56:52 for the datetime, even though the dates should be at least 2 seconds apart.

    HELP !



    var q = from x in db.Transactions select x;
               
    q = q.Where(x => x.Date >= DateTime.Now);
    Thread.Sleep(2000);
    q = q.Where(x => x.Date >= DateTime.Now);
               
    var transactions = q.Skip(0).Take(100).ToList();
                           
    SELECT TOP (100) [t0].[Id], [t0].[CreateAt], [t0].[UpdateAt], [t0].[Date]
    FROM [dbo].[TransactionModel] AS [t0]
    WHERE ([t0].[Date] >= @p0) AND ([t0].[Date] >= @p1)
    -- @p0: Input DateTime (Size = 0; Prec = 0; Scale = 0) [8/26/2008 9:56:52 PM]
    -- @p1: Input DateTime (Size = 0; Prec = 0; Scale = 0) [8/26/2008 9:56:52 PM]
    Wednesday, August 27, 2008 4:41 AM

Answers

  • Lambda expressions are actually delegates so the code in those Where conditions is not run until the query is executed, so both are effectively run at the same time in your var transactions line.

    Assigning them into two different variables and then comparing them should solve that, e.g.

    var d1 = DateTime.Now;
    q = q.Where(x => x.Date >= d1);
    Thread.Sleep(2000);
    var d2 = DateTime.Now;
    q = q.Where(x => x.Date >= d2); 


    [)amien
    Wednesday, August 27, 2008 6:33 AM
    Moderator