挂着Linq查询与Guid.Empty在where表达式

时间:2021-06-03 20:55:32

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:

我的Web应用程序,当它调用此方法时,如果我没有注释掉以下行,它总是挂起:

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

Any idea why this would be happening?

知道为什么会这样吗?

4 个解决方案

#1


2  

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

以下代码有效...有趣的是......任何想法为什么?

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

#2


0  

Try changing the code to:

尝试将代码更改为:

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

Post back if that helped...

回帖如果有帮助......

#3


0  

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

@BFree ...尝试了你的建议......并且仍然做同样的事情。奇怪的是,我可以在LinqPad中运行以下代码没有问题:

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.

就在我使用Guid.Empty的时候。简直奇怪。

#4


0  

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?

这可能是因为lambda的解释方式;在“Guid.Empty”中,“Guid.Empty”是最终lambda的一部分。我想知道LINQ提供商是否会以某种方式将此作为特殊情况处理?

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

但实际上,除了Guid和一些编译器生成的捕获类之外,这个表达式树是相同的(它们都涉及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!):

如果以上也抱怨,那么尝试启用TSQL跟踪,或者启用LINQ-providers日志记录 - 对于LINQ-to-SQL,类似下面的工作(不要引用我!):

ctx.Log = Console.Out;

#1


2  

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

以下代码有效...有趣的是......任何想法为什么?

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

#2


0  

Try changing the code to:

尝试将代码更改为:

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

Post back if that helped...

回帖如果有帮助......

#3


0  

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

@BFree ...尝试了你的建议......并且仍然做同样的事情。奇怪的是,我可以在LinqPad中运行以下代码没有问题:

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.

就在我使用Guid.Empty的时候。简直奇怪。

#4


0  

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?

这可能是因为lambda的解释方式;在“Guid.Empty”中,“Guid.Empty”是最终lambda的一部分。我想知道LINQ提供商是否会以某种方式将此作为特殊情况处理?

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

但实际上,除了Guid和一些编译器生成的捕获类之外,这个表达式树是相同的(它们都涉及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!):

如果以上也抱怨,那么尝试启用TSQL跟踪,或者启用LINQ-providers日志记录 - 对于LINQ-to-SQL,类似下面的工作(不要引用我!):

ctx.Log = Console.Out;