drjackevan
drjackevan

Reputation: 35

MVC 3 Entity Framework 4.1 get a list of unique data rows

I am using MVC 3 and Entity Framework 4.1. I need to return a view that has a list of rows of DISTINCT values from my Documents database table. In SQL Server, the query that works is as follows:

SELECT DISTINCT(DocNum), Title, DocDate, DocFileName FROM Documents

How do I do the same thing in MVC 3?

Upvotes: 0

Views: 1429

Answers (2)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364329

Try:

var query = context.Documents.Select(x => new
                 {
                     x.DocNum,
                     x.Title,
                     x.DocDate,
                     x.DocFileName 
                 }).Distinct().ToList();

Distinct must go over all returned columns otherwise you could end up with single DocNumber and for example multiple dates and query engine wouldn't know which date to select because only one record with given DocNumber can be returned.

Upvotes: 1

William Xifaras
William Xifaras

Reputation: 5312

var result = (from d in cntx.Documents
            select d).Distinct();

Upvotes: 1

Related Questions