Reputation: 35
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
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
Reputation: 5312
var result = (from d in cntx.Documents
select d).Distinct();
Upvotes: 1