执行select时出现InvalidCastException

时间:2012-08-02 14:39:11

标签: 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的select语句都可以正常工作但是这个失败了?它可以来自where子句中的声明“agentTickets.Contains(s.TicketID.Value)”吗?

3 个答案:

答案 0 :(得分:1)

尝试使用此代码

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

答案 1 :(得分:1)

如果txtUserIDTextBox(名称导致这样认为),那么如果new Guid(txtUserID.Text)不包含精确的Guid字符串({xxxxxxxx-xxxx} -xxxx-XXXX-XXXXXXXXXXXX)

答案 2 :(得分:0)

这是一个LINQ to SQL,对吗?

确保s.TicketID永远不会为NULL:

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