Home >Database >Mysql Tutorial >How to Get Distinct Results Based on a Single Field in LINQ?

How to Get Distinct Results Based on a Single Field in LINQ?

DDD
DDDOriginal
2025-01-02 13:34:401010browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn