2

在下面的代码中:

var results = from service 
              in LogisticsService.GetTransportationModes<CarrierTransportationMode>
              (
                  x => x.CarrierId == carrierRoleId && 
                       x.ParentTransportationModeId != null &&
                       !(x is LoadCarrierMode) &&
                       (x.ParentTransportationMode.TransportationModeStatus != null) &&
                       x.ParentTransportationMode.TransportationModeStatus.Value != TransportationModeStatus.Inactive
               )
                       //.OrderByDescending(o => o.IsDefault)
                       //.ThenBy(t => t.ParentTransportationMode.Name)
               orderby service.IsDefault descending, service.ParentTransportationMode.Name
               select new
               {
                   text = service.ParentTransportationMode.Name,
                   value = service.ParentTransportationMode.Id
               };

如果service.IsDefault为空,我需要完全跳过 orderby / thenby。因此代码将执行,就好像 linq 中没有 orderby 一样,如下所示:

var results = from service 
              in LogisticsService.GetTransportationModes<CarrierTransportationMode>
              (
                  x => x.CarrierId == carrierRoleId &&
                       x.ParentTransportationModeId != null &&
                       !(x is LoadCarrierMode) &&
                       (x.ParentTransportationMode.TransportationModeStatus != null) &&
                       x.ParentTransportationMode.TransportationModeStatus.Value != TransportationModeStatus.Inactive
              )
              select new
              {
                  text = service.ParentTransportationMode.Name,
                  value = service.ParentTransportationMode.Id
              };

我尝试使用如下条件修改查询:

.OrderByDescending(o => o.IsDefault.HasValue ? o.IsDefault : null)
.ThenBy(t => t.IsDefault.HasValue ? t.ParentTransportationMode.Name : null)
orderby !service.IsDefault.HasValue ? null: service.IsDefault descending, service.ParentTransportationMode.Name

但这没有帮助。

我是否需要在 orderby 中传递任何特殊参数,以便完全不会有条件地进行排序?可以使用任何像“case”这样的关键字吗?如果是这样,怎么做?

将不胜感激任何帮助!谢谢!。

4

1 回答 1

2

由于您需要保持元素的顺序service.IsDefault == null,一个简单的解决方案是将数据集分成两部分(第一service.IsDefault == null,第二:服务.IsDefault != null)对第二部分进行排序,然后,concat:

var transportationModes = LogisticsService.GetTransportationModes<CarrierTransportationMode>(x =>
                              x.CarrierId == carrierRoleId &&
                              x.ParentTransportationModeId != null &&
                              !(x is LoadCarrierMode) &&
                              (x.ParentTransportationMode.TransportationModeStatus != null) &&
                              x.ParentTransportationMode.TransportationModeStatus.Value != TransportationModeStatus.Inactive)

var services = (from service in transportationModes
                where service.IsDefault == null
                select service).Concat
               (from service in transportationModes
                where service.IsDefault != null
                orderby service.IsDefault descending, service.ParentTransportationMode.Name
                select service);

var results = from service in services
              select new
              {
                  text = service.ParentTransportationMode.Name,
                  value = service.ParentTransportationMode.Id
              };
于 2013-07-24T22:53:59.443 回答