Megha Jain Megha Jain - 1 year ago 111
SQL Question

Distinct in Linq based on only one field of the table

I am trying to use .distinct in Linq to get result based on one field of the table (so do not require a whole duplicated records from table).

I know writing basic query using distinct as followed:

var query = (from r in table1
orderby r.Text
select r).distinct();

but I need results where
is not duplicated.

Answer Source

Try this:

table1.GroupBy(x => x.Text).Select(x => x.First());

This will group the table by Text and use the first row from each groups resulting in rows where Text is distinct.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download