简体   繁体   中英

OrmLite db.Select not return response

I use Servietack and OrmLite in our project. When the request (Db.Select) is sent, after much time, no response return.

       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;
            });
        }

but when Count request, It work well

    var count = Db.Count<News>();

How do I fix this?

Calling Db.Select<News>() returns a List<News> which downloads your entire table results into an in memory list collection. Since you're trying to page the resultset you need to instead build an SqlExpression that OrmLite executes to only return the results you're interested in, eg:

//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
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM