Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQtoSQL Error: Sequence operators not supported for type 'System.String'

Tags:

linq-to-sql

For some reason my code won't work.

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

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: Sequence operators not supported for type 'System.String'

Help please?

like image 518
SpoiledTechie.com Avatar asked Nov 25 '08 16:11

SpoiledTechie.com


2 Answers

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;

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.

like image 75
James Curran Avatar answered Nov 04 '22 06:11

James Curran


James' answer is close to what I think you are wanting, if you just want the value of the ID itself go with his. If you want the object the ID is assigned to try this.

var dupes = (from tan in TANS
             group tan by tan.ID.ToString() into duplicates
             where duplicates.Count() > 1
             select duplicates.Select(d => d)).SelectMany(d => d);

Not the cleanest way to do it in LINQ I'm sure. If I come up with a cleaner way to do it I'll edit it here. That SelectMany is important as it flattens the list of objects from the IGrouping.

like image 29
David Avatar answered Nov 04 '22 04:11

David