不同的LINQ的基础上只有一个表中的字段 [英] Distinct in Linq based on only one field of the table
本文介绍了不同的LINQ的基础上只有一个表中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在LINQ to使用.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).
我知道用写基本的查询不同如下:
I know writing basic query using distinct as followed:
var query = (from r in table1
orderby r.Text
select r).distinct();
但我需要的结果,其中 r.text
不重复。
推荐答案
试试这个:
table1.GroupBy(x => x.Text).Select(x => x.First());
这将组通过表文本
并导致行,其中文本
是各组使用的第一行不同的。
This will group the table by Text
and use the first row from each groups resulting in rows where Text
is distinct.
这篇关于不同的LINQ的基础上只有一个表中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文