我有一个通用的存储库表单,我使用Autofac将依赖注入WebApi 2.0控制器。我正在构建一个GET方法,该方法接受一些可选参数,以便从安装程序的角度进行一站式过滤结果。
控制器的DI是;
public class InstallerService : EntityService<Installer>, IInstallerService
{
IContext _context;
public InstallerService(IContext context)
: base(context)
{
_context = context;
_dbset = _context.Set<Installer>();
}
我需要帮助的WebApi控制器GET方法如下;
public IHttpActionResult GetAppointments(
int id,
string policynumber = null,
bool includearchived = false,
int? pagesize = null,
int? pageoffset = null)
{
var predicate = PredicateBuilder.True<Installer>();
predicate = predicate.And(x => x.ID == id);
if (policynumber != null)
predicate = predicate.And(x => x.Appointments.Any(y => y.Vehicle.InsurancePolicyVehicles.Any(z => z.Policy.PolicyNumber.Contains(policynumber))));
if (!includearchived)
predicate = predicate.And(x => x.Archived == false);
var results = _Service.GetAppointments(predicate, pageoffset, pagesize);
var filtered = from x in results
select new
{
appointments = x.Appointments.Select(y => new
{
appointmentid = y.ID,
driverid = y.Driver.ID,
policyid = y.Vehicle.InsurancePolicyVehicles.Select(z => z.Policy.ID),
policynumber = y.Vehicle.InsurancePolicyVehicles.Select(z => z.Policy.PolicyNumber),
created = y.CreatedDate,
policyholder = y.Driver.PolicyHolder,
date = y.Date,
ampm = y.AmPm,
installer = new
{
installerid = x.ID,
name = x.Name,
contact = x.Contact,
qascore = x.QAScore,
address1 = x.Address1,
address2 = x.Address2,
address3 = x.Address3,
address4 = x.Address4,
city = x.City,
county = x.County,
postcode = x.PostCode,
country = x.Country,
email = x.Email,
web = x.Web,
archived = x.Archived,
numberofinstalls = x.NumberOfInstalls,
numberoffailedinstalls = x.NumberOfFailedInstalls
},
device = new
{
deviceid = y.Device.ID,
iccid = y.Device.ICCID,
imei = y.Device.IMEI,
msisdn = y.Device.MSISDN,
},
status = y.Status,
archived = y.Archived
})
};
var json = JsonConvert.SerializeObject(filtered);
if (json != "[]")
json = json.Replace("[{\"appointments\":", "").TrimEnd('}',']');
return Ok(json);
}
服务方法很简单;
public List<Installer> GetAppointments(Expression<Func<Installer, bool>> predicate, int? pageSize = null, int? pageIndex = null)
{
var results = _context.Installers
.Include(x => x.Appointments)
.Include(x => x.Appointments.Select(y => y.Driver))
.Include(x => x.Appointments.Select(y => y.Vehicle))
.Include(x => x.Appointments.Select(y => y.Vehicle.InsurancePolicyVehicles.Select(z => z.Policy)))
.Include(x => x.Appointments.Select(y => y.Device))
.AsExpandable()
.Where(predicate);
if (pageIndex != null && pageSize != null)
results = results.Skip((pageIndex.Value - 1) * pageSize.Value).Take(pageSize.Value);
return results.ToList();
}
我意识到这有点奇怪,但遗憾的是规范声明他们必须能够从这个角度返回数据。
如果我根据ID过滤掉,它可以正常工作,我会得到过滤后的结果列表。
但是,如果我发送一个政策号码和一个id,请参与部分或完全匹配。我得到了id的整个结果集,并且根据策略编号没有过滤掉它。
有谁能告诉我哪里出错了?