C# 执行选择时出现InvalidCastException

C# 执行选择时出现InvalidCastException,c#,.net,linq,C#,.net,Linq,我有一个如下的查询: surveyCompleted = from s in surveyCompleted where agentTickets.Contains(s.TicketID.Value) || s.UserID == new Guid(txtUserID.Text) select s; 问题是,在这段陈述之后,我无法对surveyCompleted做任何事情,因为我得到了一个InvalidCastException。你知道为什么会这样吗?所有其

我有一个如下的查询:

 surveyCompleted = from s in surveyCompleted 
                where agentTickets.Contains(s.TicketID.Value) || s.UserID == new Guid(txtUserID.Text) select s;
问题是,在这段陈述之后,我无法对surveyCompleted做任何事情,因为我得到了一个InvalidCastException。你知道为什么会这样吗?所有其他的select语句和surveyCompleted工作都很好,但是这个语句失败了?它可能来自where子句中的语句“agentTickets.Contains(s.TicketID.Value)”?

请尝试使用此代码

from s in surveyCompleted 
                where agentTickets.Contains(s.TicketID.Value) || s.UserID == new Guid(txtUserID.Text).ToString() select s;

这是LINQ到SQL的转换,对吗

确保
s.TicketID
从不为空:

!Convert.IsDBNull(s.TicketID) && agentTickets.Contains(s.TicketID.Value)

如果
txtserid
是一个
TextBox
(名字让人这么想),那么
新的Guid(txtserid.Text)
如果不包含精确的
Guid
类字符串(xxxxxxxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxxxxxxxxxxxx)

你从哪里得到异常?请演示如何使用
surveyCompleted
。它在surveyCompleted.ToList()上崩溃,或者当我尝试在Visual studio调试模式下枚举集合时,仍然会得到异常,异常中是否还有其他详细信息?基本上,请给我们尽可能多的信息。(我们目前甚至不知道这是LINQ到对象还是LINQ到SQL等等。)LINQ到SQL,在下面的一篇博文中,它提到了由于新的Guid()而发生的崩溃,该Guid()延迟到查询被枚举为止。您能不能发布异常的实际文本和堆栈跟踪?我对LINQ很陌生,但我相信您是对的,当我创建Guid时,它应该崩溃,但它被延迟,并且在我尝试枚举查询时崩溃。这是查询和方法实际执行的时刻