gout
gout

Reputation: 812

Getting distinct rows in LINQ

I need to get the distinct row values from a table using 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. What should be the query to achieve this?

Its sql equivalent is select distinct statename from dbo_statetable

Upvotes: 0

Views: 745

Answers (2)

tech-man
tech-man

Reputation: 3246

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

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838106

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. It is like a table alias in SQL. Your LINQ statement is roughly equivalent to this SQL:

SELECT DISTINCT * FROM dbo_statetable AS statename

Upvotes: 1

Related Questions