Explore Courses Blog Tutorials Interview Questions
0 votes
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
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.5k answers


108k users

Browse Categories