我正在编写一个get请求api,我想从数据库中获取记录,然后在视图模型中分配选定的字段。 请教我如何使用投影创建查询并为模型分配值
我创建了一个perent模型,该模型对其所有子实体都具有导航属性。 使用dbcontext,我已经成功地将数据分配到变量中,而将其分配给模型约束错误。 还请告诉我如何获取每种津贴类型比率下所有雇员的总和
处理程序代码
try
{
var predicate = PredicateBuilder.True<PreRevisionRegularEmployee>();
var model = new PayBillListView();
int SectionId = request.SectionId;
PayBillViewModel payBillViewModel = new PayBillViewModel();
DateTime paydate = string.IsNullOrEmpty(request.Date) ? DateTime.Now : Convert.ToDateTime(request.Date);
predicate = predicate.And(x => x.BasicDetail.SectionId == SectionId);
IList<PayBillViewModel> payBill = await _context.PreRevisionRegularEmployees.Where(predicate)
.Select(c => new PayBillViewModel
{
PreRevisionRegularEmployeeId = c.PreRevisionRegularEmployeeId,
CPFNumber = c.CPFNumber,
GPFNumber = c.GPFNumber,
percentageAllowanceTypeName = c.PreRevisionRegularEmployeePercentageAllowanceTypes.Select(pa => pa.PercentageAllowanceType.Name),
PercentageAllowanceRate =(IEnumerable<PercentageAllowance>) c.PreRevisionRegularEmployeePercentageAllowanceTypes.Select(par => par.PercentageAllowanceType.PercentageAllowances.Select(p=>p.Rate)),
Probation = c.BasicDetail.Probation
})
.ToListAsync();
model.payBillViewModels = _mapper.Map <IList<PayBillViewModel>>(payBill);
return model;
}
catch (Exception)
{
throw;
}
主实体
public class PreRevisionRegularEmployee: BaseEntity
{
public int PreRevisionRegularEmployeeId { get; set; }
//public DateTime CreatedAt { get; set; }
//public string CreatedBy { get; set; }
//public DateTime ModifiedAt { get; set; }
//public string ModifiedBy { get; set; }
public string GPFNumber { get; set; }
public string CPFNumber { get; set; }
public string GospedCode { get; set; } // 14-digit code
public string EmployeeCode
{
get
{
return string.IsNullOrEmpty(GPFNumber) ?
(string.IsNullOrEmpty(CPFNumber) ? GospedCode : CPFNumber) : GPFNumber;
}
}
public int EmployeeClass { get; set; }
public bool GovernmentAccomodation { get; set; }
public string AccomodationClass { get; set; }
public bool VehicleAttached { get; set; }
public BasicDetail BasicDetail { get; set; }
public AppointmentOrder AppointmentOrder { get; set; }
public ICollection<PreRevisionRegularEmployeePercentageAllowanceType> PreRevisionRegularEmployeePercentageAllowanceTypes { get; set; }
public class PreRevisionRegularEmployeePercentageAllowanceType
{
public int PreRevisionRegularEmployeeId { get; set; }
public PreRevisionRegularEmployee PreRevisionRegularEmployee { get; set; }
public int PercentageAllowanceTypeId { get; set; }
public PercentageAllowanceType PercentageAllowanceType { get; set; }
}
public class PercentageAllowanceType : BaseLookup
{
public int PercentageAllowanceTypeId { get; set; }
public ICollection<PercentageAllowance> PercentageAllowances { get; set; }
public ICollection<PreRevisionRegularEmployeePercentageAllowanceType> PreRevisionRegularEmployeePercentageAllowanceTypes { get; set; }
}
public class PercentageAllowance : BaseEntity
{
public int PercentageAllowanceId { get; set; }
public int RevisedPayRevision { get; set; }
public DateTime EffectiveDateFrom { get; set; }
public float Rate { get; set; }
public int PercentageAllowanceTypeId { get; set; }
public PercentageAllowanceType PercentageAllowanceType { get; set; }
}