feather
feather

Reputation: 85

orderby() and distinct() in LINQ

The following line doesn't work as distinct() doesn't give ordered output.

var context= new BatchEntities();//Entity model
var practices = (from p in context.EMSCAN_BATCH orderby  p.PRACTICE select p.PRACTICE).Distinct();

Tried this:

var practices = (from p in context.EMSCAN_BATCH
                             select p.PRACTICE).Distinct().OrderBy(x=>x.PRACTICE);

This gives error that string doesn't contain definition for 'PRACTICE'.... Also tried this:

 var practices =(from p in context.EMSCAN_BATCH
                select new
                {
                  p.PRACTICE
                }).Distinct().OrderBy(x=>x.PRACTICE);

This gives wrong output.

Upvotes: 0

Views: 537

Answers (2)

Louis Michael
Louis Michael

Reputation: 398

Or you can do something like this:

var practices = context.EMSCAN_BATCH.SELECT(m => m.PRACTICE).Distinct().OrderBy(m => m.PRACTICE);

Upvotes: 0

Selman Genç
Selman Genç

Reputation: 101701

Just remove the PRACTICE from OrderBy

var practices = (from p in context.EMSCAN_BATCH
                 select p.PRACTICE).Distinct().OrderBy(x => x);

Upvotes: 1

Related Questions