【问题标题】:How to Convert Sql to Linq with Distinct(id)如何使用 Distinct(id) 将 Sql 转换为 Linq
【发布时间】:2019-04-05 12:04:20
【问题描述】:

从 MT_Vehicle 中选择 count(distinct LicencePlate),其中 IsDeleted=0 且 CreatedBy = 1

【问题讨论】:

    标签: sql linq linq-to-sql distinct


    【解决方案1】:

    你可以这样写:

    var count = db.MT_Vehicle
        .Where( v => v.IsDeleted == 0 && v.CreatedBy == 1 )
        .Select(v => v.LicencePlate)
        .Distinct()
        .Count();
    

    【讨论】:

      【解决方案2】:
      var count = MT_Vehicle.Where(x => x.IsDeleted==0 && x.CreatedBy == 1)
                            .Select(x => x.LicencePlate)
                            .Distinct()
                            .Count();
      

      【讨论】:

      • 所以?你会期待什么?
      • 我想根据车牌显示没有重复的车辆总数,并且不显示已删除的车辆
      • 你知道我没有魔法,对吧?据我所知,您的桌子可能是empty。所以根据你的描述,你应该删除过滤“CreatedBy”的部分。
      猜你喜欢
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      相关资源
      最近更新 更多