2016-09-20 82 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, 
} 
相关问题