【问题标题】:Order results by the length of varchar/nvarchar column value in EF Core按 EF Core 中 varchar/nvarchar 列值的长度对结果进行排序
【发布时间】:2021-04-22 21:55:31
【问题描述】:

如何按 EF Core 中列值的长度对结果集进行排序?

其中一个实体属性是barcode,我需要将具有较短条形码的实体放在列表顶部,同时搜索值的可能性不完全匹配。

在 SQL 中:

Select * 
From dbo.Barcodes 
Where BarcodeValue Like '%221%'
Order By Len(BarcodeValue) Asc

如何在 EF Core 中执行此操作?

【问题讨论】:

    标签: c# entity-framework-core ef-core-3.1


    【解决方案1】:
    dbo.Barcodes.Where(x => x.BarcodeValue.Contains("221")).OrderBy(x=> x.BarcodeValue.Length)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-25
      • 1970-01-01
      • 2017-11-24
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      相关资源
      最近更新 更多