如何使用 AutoQuery 执行更复杂的查询 [英] How to perform a more complex query with AutoQuery
问题描述
鉴于来自 ServiceStack 端点的以下定义:
Given the following definitions from a ServiceStack endpoint:
public class LoanQueue
{
public int LoanId { get; set; }
public DateTime Submitted { get; set; }
public DateTime Funded { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Fico { get; set; }
public int Fraud { get; set; }
public int CDS { get; set; }
public int IDA { get; set; }
public string Income { get; set; }
public string Liabilities { get; set; }
public string Agent { get; set; }
public string Status { get; set; }
public string State { get; set; }
public string Product { get; set; }
public string Comment { get; set; }
}
public enum DateType
{
None,
Submitted,
Funded
}
[Route("/loan/queue/search", "GET")]
public class LoanQueueQueryGet : QueryBase<LoanQueue>
{
public DateType DateType { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string AgentUserName { get; set; }
public Languange Languange { get; set; }
public bool WorkingLoan { get; set; }
public bool MicrobusinessLoan { get; set; }
public LoanStatus LoanStatus { get; set; }
}
public object Get(LoanQueueQueryGet request)
{
if (request == null) throw new ArgumentNullException("request");
var profiler = Profiler.Current;
using (profiler.Step("LoanServices.LoanQueue"))
{
SqlExpression<LoanQueue> q = AutoQuery.CreateQuery(request, Request.GetRequestParams());
QueryResponse<LoanQueue> loanQueueResponse = AutoQuery.Execute(request, q);
return loanQueueResponse;
}
}
我的问题是,甚至可以根据服务 impl 中的请求对象运行条件逻辑"?例如
My question is this, "Is it even possible to run conditional logic based on the request object in the service impl"? e.g.
如果 DateType == DateType.Submitted
If DateType == DateType.Submitted
然后使用 BETWEEN 子句 (StartDate/EndDate) 或
then query the Submitted property on the LoanQueue with a BETWEEN clause (StartDate/EndDate) or
如果 DateType == DateType.Funded
If DateType == DateType.Funded
然后使用 BETWEEN 子句 (StartDate/EndDate) 查询 LoanQueue 上的 Funded 属性.
then query the Funded property on the LoanQueue with a BETWEEN clause (StartDate/EndDate).
我的猜测是我试图将 AutoQuery 弯曲得太远,如果只用旧时尚的方式对其进行编码会更好.我真的很喜欢 AutoQuery 插件的内置功能,我相信它有时会满足我的需求.
My guess is that I'm trying to bend AutoQuery too far and would be better served just coding it up the old fashion way. I really like the baked-in features of the AutoQuery plugin and I'm sure there will be times when it will suit my needs.
谢谢,斯蒂芬
推荐答案
AutoQuery 将忽略任何不匹配的字段,以便您可以使用它们通过附加自定义逻辑扩展填充的 AutoQuery,例如:
AutoQuery will ignore any unmatched fields so you're able to use them to extend your populated AutoQuery with additional custom logic, e.g:
public object Get(LoanQueueQueryGet request)
{
var q = AutoQuery.CreateQuery(request, Request.GetRequestParams());
if (request.DateType == DateType.Submitted)
{
q.And(x => x.Submitted >= request.StartDate && x.Submitted < request.EndDate);
}
else
{
q.And(x => x.Funded >= request.StartDate && x.Funded < request.EndDate);
}
var loanQueueResponse = AutoQuery.Execute(request, q);
return loanQueueResponse;
}
这篇关于如何使用 AutoQuery 执行更复杂的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!