如何使用 Distinct(id) 将 Sql 转换为 Linq

How to Convert Sql to Linq with Distinct(id)

select 从 MT_Vehicle 计数(不同的 LicencePlate),其中 IsDeleted=0 和 CreatedBy = 1

你可以这样写:

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