2016-09-20 36 views
0

在數據庫表中的「設備」有一欄「狀態」(整數)排序映射字符串,而不是整數

 
Name | Status 
---------------- 
Device1  1 
Device2  2 
Device3  3 
Device4  4 
Device5  3 

在我的應用我有映射列「狀態」,以人類可讀的話(串)

public enum Status 
{ 
    Start = 1, 
    Stop = 2, 
    Running = 3, 
    new Device = 4, 
} 

如果我按「狀態」命令,結果將按整數排序。

_repository.Query<Device>().OrderBy(c=>c.status) 
          .Skip(skip) 
          .Take(500); 

在表中 「設備」 我有超過60.000的記錄,所以我使用分頁

結果:

 
Name  | Status 
------------------ 
Device1  Start 
Device2  Stop 
Device3  Running 
Device5  Running 
Device4  new Device 

我需要什麼:

 
Name  | Status 
------------------ 
Device4  new Device 
Device3  Running 
Device5  Running 
Device1  Start 
Device2  Stop 

什麼我可不可以做?

回答

1

您可以使用條件運算符:

var query = _repository.Query() 
    .OrderBy(c => c.Status == Status.NewDevice ? 0 : c.Status == Status.Running ? 1 : c.Status == Status.Start ? 2 : 3) 
    .Skip(skip) 
    .Take(500); 
+0

感謝,這個偉大工程 – supersonic

0

也許更改號碼?

public enum Status 
{ 
    Start = 3, 
    Stop = 4, 
    Running = 2, 
    new Device = 1, 
} 
相關問題