views:

722

answers:

4

I'm having an issue with the following code:

    private void DataPortal_Fetch(TaskCriteria criteria)
    {
        using (var ctx = ContextManager<Gimli.Data.GimliDataContext>
                    .GetManager(Database.ApplicationConnection, false))
        {
            this.RaiseListChangedEvents = false;
            this.IsReadOnly = false;

            IQueryable<Data.Task> query = ctx.DataContext.Tasks;

            if (criteria.ReadyForPricing)
            {
                query = query.Where(row => row.IsPriced != true);
                query = query.Where(row => row.Status == (int)TaskStatus.Closed);
                query = query.Where(row => row.InvoiceId == Guid.Empty);
            }

            if (criteria.ReadyForInvoicing)
            {
                query = query.Where(row => row.IsPriced == true);
                query = query.Where(row => row.Status == (int)TaskStatus.Closed);
                query = query.Where(row => row.InvoiceId == Guid.Empty);
            }

            var data = query.Select(row => TaskInfo.FetchTaskInfo(row));

            this.AddRange(data);

            this.IsReadOnly = true;
            this.RaiseListChangedEvents = true;
        }
    }

My web application, when it calls this method, always hangs if I don't comment out the following line:

query = query.Where(row => row.InvoiceId == Guid.Empty

Any idea why this would be happening?

+1  A: 

Try changing the code to:

query.Where(row => object.Equals(row.InvoiceId, Guid.Empty))

Post back if that helped...

BFree
Thanks! I'll give it a try!
mattruma
Still hangs ... really odd. I can run the same query in LinqPad and it works just fine.
mattruma
Hmm, figured it was worth a shot. I got nothing else.. sorry!
BFree
Thanks ... I appreciate it! Got the following to work new Guid("00000000-0000-0000-0000-000000000000") ... instead of Guid.Empty.
mattruma
A: 

@BFree ... Tried what you suggested ... and still do the same thing. It's odd, I can run the following code in LinqPad with no problem:

from t in Tasks
where  t.IsPriced == false
&& t.IsNotInvoiceable == false
&& t.Status == 5
&& t.InvoiceId == Guid.Empty
select t

As well as I can use the following line of code with not problems either:

if (criteria.ProjectId != Guid.Empty)
     query = query.Where(row => row.ProjectId == criteria.ProjectId);

It's just when I use Guid.Empty. Just plain odd.

mattruma
+1  A: 

The following code works ... interestingly enough ... any idea of why?

query = query.Where(row => row.InvoiceId == new Guid("00000000-0000-0000-0000-000000000000"));
mattruma
What happens when you run a SQL Server Profiler trace? Do the two queries look different on the SQL server end?
Dave Markle
Good question ... I'm going to check that out.
mattruma
A: 

It could be because of how the lambda is interpreted; with "Guid.Empty", the "Guid.Empty" is part of the final lambda. I wonder whether the LINQ-provider is treating this as a special case somehow?

You could try:

Guid empty = Guid.Empty;
query = query.Where(row => row.InvoiceId == empty);

But actually, other than Guid vs some compiler-generated capture class, the expression tree for this is the same (they both involve lambda=>BinaryExpression=>MemberExpression).

If the above also complains, then try putting a TSQL trace on, or enabling your LINQ-providers logging - for LINQ-to-SQL, something like below works (don't quote me!):

ctx.Log = Console.Out;
Marc Gravell
Thanks Marc! I'll give this a try!
mattruma