C# 导致此错误的原因:类型';不支持序列运算符;System.String';

C# 导致此错误的原因:类型';不支持序列运算符;System.String';,c#,asp.net,C#,Asp.net,这是我的密码: IEnumerable<DataAccessLayer.SQLPendingEventRecord> dataset1 = _v3Context.SQLPendingEvents .Where(sp => sp.EventType.ToString() == str100w && sp.EventDateTime > dtStartDate && !sp.KeyDeviceI

这是我的密码:

    IEnumerable<DataAccessLayer.SQLPendingEventRecord> dataset1 = _v3Context.SQLPendingEvents
                     .Where(sp => sp.EventType.ToString() == str100w && sp.EventDateTime > dtStartDate && !sp.KeyDeviceID.ToString().Contains('~'))
                     .AsEnumerable() // further processing occurs on client
                     .Select((sp, index) => new DataAccessLayer.SQLPendingEventRecord
                     {
                         ID = index + 1,
                         KeyDeviceID=sp.KeyDeviceID,
                         UpdateTime=sp.UpdateTime,
                         EventClass=sp.EventClass,
                         EventCode=sp.EventCode,
                         EventType=sp.EventType,
                         EventDateTime=Convert.ToDateTime(sp.EventDateTime),
                         KeyPropertyID=Convert.ToInt32 (sp.KeyPropertyID),
                         KeyEntityID=Convert.ToInt32 (sp.KeyEntityID),
                         EventText=sp.EventText,
                         KeyUserID=sp.KeyUserID,
                         //sp.EventImage,
                         TaskType=sp.TaskType,
                         TaskSubType=sp.TaskSubType,
                         UniqueTaskID=sp.UniqueTaskID
                     }).ToList();
IEnumerable dataset1=_v3Context.SQLPendingEvents
其中(sp=>sp.EventType.ToString()==str100w&&sp.EventDateTime>dtStartDate&&sp.KeyDeviceID.ToString()。包含(“~”)
.AsEnumerable()//在客户端上进行进一步处理
.选择((sp,索引)=>new DataAccessLayer.SQLPendingEventRecord
{
ID=索引+1,
KeyDeviceID=sp.KeyDeviceID,
UpdateTime=sp.UpdateTime,
EventClass=sp.EventClass,
EventCode=sp.EventCode,
EventType=sp.EventType,
EventDateTime=Convert.ToDateTime(sp.EventDateTime),
KeyPropertyID=Convert.ToInt32(sp.KeyPropertyID),
KeyEntityID=Convert.ToInt32(sp.KeyEntityID),
EventText=sp.EventText,
KeyUserID=sp.KeyUserID,
//sp.EventImage,
TaskType=sp.TaskType,
TaskSubType=sp.TaskSubType,
UniqueTaskID=sp.UniqueTaskID
}).ToList();
我刚才补充说:&&!sp.KeyDeviceID.ToString()包含(“~”) 现在,我得到了奇怪的错误消息:“System.String”类型不支持序列运算符。 在那之前,我没有问题。 现在,我得到了这个神秘的错误信息。 由于数据库的大小,我需要将这一切都放在一个查询中。
有什么想法吗?

我怀疑问题在于它使用的是
可枚举.Contains(string,char)
(因为
string
实现了
IEnumerable
),而您真正想要的是

只是将其更改为:

!sp.KeyDeviceID.ToString().Contains("~")
可以解决这个问题

请注意,如果
KeyDeviceID
已经是一个字符串属性,我将放弃
ToString()
调用:

!sp.KeyDeviceID.Contains("~")

您正在使用LINQtoSQL吗?Linq to SQL不支持数据类型的所有.Net功能。您可以使用SQLFunctions来复制某些功能。如果sp.KeyDeviceID不是字符串,那么如果使用Linq to SQL,则使用ToString()将引发错误。 试一试

IEnumerable dataset1=_v3Context.SQLPendingEvents
.Where(sp=>sp.EventType.ToString()==str100w&&sp.EventDateTime>dtStartDate&!**SQLFunctions.CharIndex(sp.KeyDeviceID,“~”)>0)**
.AsEnumerable()//在客户端上进行进一步处理
.选择((sp,索引)=>new DataAccessLayer.SQLPendingEventRecord
{
ID=索引+1,
KeyDeviceID=sp.KeyDeviceID,
UpdateTime=sp.UpdateTime,
EventClass=sp.EventClass,
EventCode=sp.EventCode,
EventType=sp.EventType,
EventDateTime=Convert.ToDateTime(sp.EventDateTime),
KeyPropertyID=Convert.ToInt32(sp.KeyPropertyID),
KeyEntityID=Convert.ToInt32(sp.KeyEntityID),
EventText=sp.EventText,
KeyUserID=sp.KeyUserID,
//sp.EventImage,
TaskType=sp.TaskType,
TaskSubType=sp.TaskSubType,
UniqueTaskID=sp.UniqueTaskID
}).ToList();

对于您正在使用的提供程序,
Contains()
可能没有SQL等价物。
KeyDeviceId
的类型是什么?这行代码工作正常:dataset1=dataset1.Where(sp=>!sp.KeyDeviceId.ToString().Contains(“~”).ToList();KeyDeviceId是一个字符串。那你为什么要对它调用
ToString
?@Stevestuple:我怀疑这会产生与
'~'
参数相同的错误。(你发现我改变了文字类型了吗?)我把“~”放在一个常量中,这样做了:string strTilde=“~”;IEnumerable dataset1=_v3Context.SQLPendingEvents.Where(sp=>sp.EventType.ToString()==str100w&&sp.EventDateTime>dtStartDate&!sp.KeyDeviceID.Contains(strilde))@Stevestype:通过做什么?(请记住,堆栈溢出不仅仅是为了帮助您,它是为了帮助以后遇到相同问题的其他人。)Jon Skeet-Enter按钮在我准备好之前保存了我的消息。没有新线@史蒂夫斯塔普:听起来很可疑-你绝对确定当你使用字符串文字时它失败了吗?您可能还没有使用字符文字来运行代码?新问题已经得到了回答。
IEnumerable<DataAccessLayer.SQLPendingEventRecord> dataset1 = _v3Context.SQLPendingEvents
                     .Where(sp => sp.EventType.ToString() == str100w && sp.EventDateTime > dtStartDate && !**SQLFunctions.CharIndex(sp.KeyDeviceID,"~")>0)**
                     .AsEnumerable() // further processing occurs on client
                     .Select((sp, index) => new DataAccessLayer.SQLPendingEventRecord
                     {
                         ID = index + 1,
                         KeyDeviceID=sp.KeyDeviceID,
                         UpdateTime=sp.UpdateTime,
                         EventClass=sp.EventClass,
                         EventCode=sp.EventCode,
                         EventType=sp.EventType,
                         EventDateTime=Convert.ToDateTime(sp.EventDateTime),
                         KeyPropertyID=Convert.ToInt32 (sp.KeyPropertyID),
                         KeyEntityID=Convert.ToInt32 (sp.KeyEntityID),
                         EventText=sp.EventText,
                         KeyUserID=sp.KeyUserID,
                         //sp.EventImage,
                         TaskType=sp.TaskType,
                         TaskSubType=sp.TaskSubType,
                         UniqueTaskID=sp.UniqueTaskID
                     }).ToList();