我有这个模型,它为请求后的正文提供了可选的参数:
public class ReportFilterModel { public int? ReportStatusId { get; set; } public Guid? AreaID { get; set; } }
这将根据用户提供的过滤器在数据库中进行搜索:
var report = _context.tbl_Reports.AsQueryable(); if (model.ReportStatusId.IsNotNull()) report = report.Where(x => x.IsDeleted == false && x.ReportStatusId == (StatusEnum)model.ReportStatusId.Value); if (model.Area.IsNotNull()) report = report.Where(x => x.IsDeleted == false && x.Area.Id == model.AreaId);
然后它最终将返回此:
finalReports = await report .Where(x => x.IsDeleted == false) .Select(x => new ReportsWithFiltersModel { Id = x.Id, Area = Id, UserId = x.UserId, ReportStatus = x.ReportStatusId.GetEnumDescription(), }).ToListAsync();
所有这些工作都很好,但是现在我想列出这样的可选参数列表:
public class ReportFilterModel { public List<int>? ReportStatusId { get; set; } public List<Guid>? AreaID { get; set; } }
这是个例外
'List<int>' does not contain a definition for 'Value' and no accessible extension method 'Value' accepting a first argument of type 'List<int>' could be found (are you missing a using directive or an assembly reference?)
我该如何解决?
我有这个模型,它为邮寄请求正文使用可选参数:public class ReportFilterModel {public int? ReportStatusId {组; }公共向导? AreaID {get;组; }} ...
?不代表可选。它表示Nullable,其中T是值类型(适用于值类型并使它们为可空)。
您正在混合两个概念nullable value types和nullable reference types(在C#8.0中添加)。标有?
的Nullabale值类型基本上是Nullable<T>
的快捷方式,并且具有Nullable<T>
属性。