Hanging Linq query with Guid.Empty in the where expression

坚强是说给别人听的谎言 提交于 2019-12-06 02:08:47

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

query = query.Where(row => row.InvoiceId == new Guid("00000000-0000-0000-0000-000000000000"));

Try changing the code to:

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

Post back if that helped...

@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.

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!