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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With