简体   繁体   English

在LINQ中获取不同的行

[英]Getting distinct rows in LINQ

I need to get the distinct row values from a table using LINQ. 我需要使用LINQ从表中获得不同的行值。 The query i used is 我使用的查询是

 var results = (from statename in dsobject.dbo_statetable select statename).Distinct();

It is giving all the rows of the table named "dbo_statetable" but i need only the distinct statename. 它给出了名为“ dbo_statetable”的表的所有行,但我只需要唯一的状态名。 What should be the query to achieve this? 实现该目标的查询应该是什么?

Its sql equivalent is select distinct statename from dbo_statetable 它的sql等效项是select distinct statename from dbo_statetable

You need to specify the property: 您需要指定属性:

var results = (from x in dsobject.dbo_statetable select x.Statename).Distinct();
//                                                       ^^^^^^^^^^

The variable after from does not specify the column. from之后的变量未指定列。 It is like a table alias in SQL. 就像SQL中的表别名一样。 Your LINQ statement is roughly equivalent to this SQL: 您的LINQ语句大致等效于以下SQL:

SELECT DISTINCT * FROM dbo_statetable AS statename

dsobject.dbo_statetable.Select(s => s.statename).Distinct()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM