Intellipaat 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
2 answers
0 votes
1 answer
asked Oct 5, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
asked Oct 5, 2019 in SQL by Tech4ever (20.3k points)
0 votes
3 answers

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...