LINQtoSQL错误:类型'System.String'不支持序列运算符 [英] LINQtoSQL Error: Sequence operators not supported for type 'System.String'

查看:278
本文介绍了LINQtoSQL错误:类型'System.String'不支持序列运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我的代码无法正常工作.

For some reason my code won't work.

    from tan in TANS
    where tan.ID.ToString().Count() != 1
    select tan

我想选择表中所有重复的ID,所以我使用count!= 1并收到此错误.

I want to select all IDs that are duplicates in a table so I am using the count != 1 and I get this error.

NotSupportedException:类型'System.String'不支持序列运算符

NotSupportedException: Sequence operators not supported for type 'System.String'

请帮助?

推荐答案

tan.ID.ToString()是字符串,而不是集合,因此您无法应用Count().

tan.ID.ToString() is a string, not a collection so you can't apply Count().

我相信您想要这样的东西:(此语法是错误的,但是很接近)

I believe you want something like: (This syntax is wrong, but close)

from tan in TANS
group tan by tan.ID into dups
where dups.Count() > 1
select dups.Key;

更新(5年减去5天后):(对Google问题有点奇怪,找到您所写的答案..)此问题的核心是LINQ语句正在尝试构建SQL语句,并且数据库不知道如何将Count()应用于字符串.但是,如果对内存中的集合使用LINQ,则它将字符串视为IEnumerable,而Count()则可以正常工作.

Update (after 5 years minus 5 days): (It's a bit weird to Google a problem and find an answer YOU wrote ..) At the core of this problem is the the LINQ statement is trying to build an SQL statement, and the database doesn't know how to apply Count() to a string. However, if you use LINQ against a collection in memory, then it would treat the string as an IEnumerable and the Count() would work just fine.

这篇关于LINQtoSQL错误:类型'System.String'不支持序列运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆