views:

979

answers:

3

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?

+3  A: 

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 where dups.Count() > 1
select dups.Key;
James Curran
+2  A: 

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.

David
A: 

I got it working with James answer.

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

Thanks guys, much appreciated.

Scott