Home >Database >Mysql Tutorial >How to Get Distinct Results Based on a Single Field in LINQ?
Distinct Results Based on a Specific Field in Linq
Problem:
Obtain distinct results from a database table using Linq based solely on a single field, without retrieving the entire duplicate records.
Answer: Group by Selected Field and Return First Instance
To achieve this, utilize the following query:
table1.GroupBy(x => x.Text).Select(x => x.FirstOrDefault());
This query groups the table by its Text field. Within each group, it selects the first row. Consequently, only unique Text values will be returned as results.
The above is the detailed content of How to Get Distinct Results Based on a Single Field in LINQ?. For more information, please follow other related articles on the PHP Chinese website!