我在项目中使用Servietack和OrmLite。发送请求(Db.Select)后,经过很长时间后,没有响应返回。
public object Get(GetCategoryNews request)
{
var PageCount = 20;
var skip = (request.Page.GetValueOrDefault(1) - 1) * PageCount;
var cacheKey = UrnId.Create<GetCategoryNews>(DateTime.Now.ToString("yyMMddhhmm") + skip);
return base.Request.ToOptimizedResultUsingCache(base.Cache, cacheKey, () =>
{
//On this line, waiting and no response
var status = Db.Select<News>().Skip(skip).Take(PageCount).Select(o => new NewsResponse() {
Id = o.Id,
CreateOn = o.CreateOn,
Description = o.Description,
PublishOn = o.PubDate,
Title = o.Title
});
return status;
});
}
但是当Count请求时,它运作良好
var count = Db.Count<News>();
我该如何解决这个问题?
答案 0 :(得分:2)
调用Db.Select<News>()
会返回List<News>
,将整个表格结果下载到内存列表集合中。由于您正在尝试分页结果集,因此您需要构建一个SqlExpression
,OrmLite执行该//Create SQL Query
var q = db.From<News>()
.Skip(skip)
.Take(PageCount);
var results = db.Select(q); //Executes paged query
return results.Select(o => new NewsResponse {
Id = o.Id,
CreateOn = o.CreateOn,
Description = o.Description,
PublishOn = o.PubDate,
Title = o.Title
});
仅返回您感兴趣的结果,例如:
OnInitialized