2014-10-22 104 views
0

以下行不起作用,因爲distinct()不提供有序輸出。LINQ中的orderby()和distinct()

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

嘗試這樣做:

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

這給了錯誤的字符串不包含定義 '實踐' .... 也試過這樣:

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

這給了錯誤的輸出。

回答

1

只需卸下PRACTICEOrderBy

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

或者你也可以做這樣的事情:

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