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
Release: 2025-01-02 13:34:40
Original
1016 people have browsed it

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());
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template