Domanda

I have a situation where my user is presented with a grid, and it, by default, will just get the first 15 results. However they may type in a name and search for an item across all pages.

Alone, either of these works fine, but I am trying to figure out how to make them work as a single query. This is basically what it looks like...

// find a filter if the user is searching
var filters = request.Filters.ToFilters();

// determine the name to search by
var name = filters.Count > 0 ? filters[0] : null;

// we need to be able to catch some query statistics to make sure that the
// grid view is complete and accurate
RavenQueryStatistics statistics;

// try to query the items listing as quickly as we can, getting only the
// page we want out of it
var items = RavenSession
    .Query<Models.Items.Item>()
    .Statistics(out statistics) // output our query statistics
    .Search(n => n.Name, name)
    .Skip((request.Page - 1) * request.PageSize)
    .Take(request.PageSize)
    .ToArray();

// we need to store the total results so that we can keep the grid up to date
var totalResults = statistics.TotalResults;

return Json(new { data = items, total = totalResults }, JsonRequestBehavior.AllowGet);

The problem is that if no name is given, it does not return anything; Which is not the desired result. (Searching by 'null' doesn't work, obviously.)

È stato utile?

Soluzione

Do something like this:

var q= RavenSession
    .Query<Models.Items.Item>()
    .Statistics(out statistics); // output our query statistics

if(string.IsNullOrEmpty(name) == false)
    q = q.Search(n => n.Name, name);


var items = q.Skip((request.Page - 1) * request.PageSize)
    .Take(request.PageSize)
    .ToArray();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top