仅基于表的一个字段在 Linq 中有所不同 [英] Distinct in Linq based on only one field of the table

查看:20
本文介绍了仅基于表的一个字段在 Linq 中有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Linq 中使用 .distinct 来获取基于表的一个字段的结果(因此不需要表中的整个重复记录).

I am trying to use .distinct in Linq to get result based on one field of the table (so do not require a whole duplicated records from table).

我知道使用 distinct 编写基本查询,如下所示:

I know writing basic query using distinct as followed:

var query = (from r in table1
orderby r.Text
select r).distinct();

但我需要 r.text 不重复的结果.

but I need results where r.text is not duplicated.

推荐答案

试试这个:

table1.GroupBy(x => x.Text).Select(x => x.FirstOrDefault());

这将按 Text 对表格进行分组,并使用每个组的第一行,从而生成 Text 不同的行.

This will group the table by Text and use the first row from each groups resulting in rows where Text is distinct.

这篇关于仅基于表的一个字段在 Linq 中有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆