Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

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

I know writing a basic query using distinct as followed:

var query = (from r in table1

orderby r.Text

select r).distinct();

but I need results where r.text is not duplicated.

1 Answer

0 votes
by (40.7k points)

Try using the code given below:

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

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

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 9, 2019 in Java by Krishna (2.6k points)
0 votes
1 answer

Browse Categories

...