Find All Rows in DataTable Where Column Value is NOT Unique Using Linq Query

1k views Asked by At

I am attempting to write a Linq to SQL query that returns all rows in a DataTable where a columns value (TxnNumber) is not unique. So far, I have the Linq to SQL below where dt is the DataTable that contains the field TxnNumber. I think that I am pretty close but, intellisense is complaining about the CONTAINS clause. I have tried specifying that I only want to return the TxnNumber field in the sub-select but, it will not compile. Can anyone see what I am doing wrong?

dt.AsEnumerable().Where(u => u.TxnNumber.Contains (dt.AsEnumerable().GroupBy(t => t.TxnNumber).Count() > 1));
1

There are 1 answers

0
Oleg On BEST ANSWER

Try this

(from r in dt.AsEnumerable()
group r by r.TxnNumber into grp
where grp.Count() > 1
select grp).SelectMany(x=>x).ToList();