-
Notifications
You must be signed in to change notification settings - Fork 2
13 ByStepQuery
In the previous workshop you saw how to use PaginatedQuery.
There is another strategy that can be used for long list that is often used with search: the loading by step.
So, in order to avoid to wait a long time when you are loading a list that might contain thousands elements, you can load them 100 by 100 with a stop button and when you type a new character on your search, you stop the previous query and start a new one.
So in this workshop you will modify the previous popup to use this new one:
WAQS provides a mechanism similar to the PaginatedQuery for thos scenario.
Indeed, you can call the method ToByStepQuery on WAQS LINQ queries.
So in the workshop, you search products that contains a text set in a specific TextBox.
First, update the ProductsWindow.xaml with this xaml.
Then replace the ProductsPaginatedQuery by this:
private ByStepQuery<Product> _productsByStepQuery;
public ByStepQuery<Product> ProductsByStepQuery
{
get { return _productsByStepQuery; }
private set
{
_productsByStepQuery = value;
NotifyPropertyChanged.RaisePropertyChanged(nameof(ProductsByStepQuery));
}
}
Then define the FullNextSearch property.
private string _fullNameSearch;
public string FullNameSearch
{
get { return _fullNameSearch; }
set
{
_fullNameSearch = value;
NotifyPropertyChanged.RaisePropertyChanged(nameof(FullNameSearch));
if (ProductsByStepQuery != null)
{
ProductsByStepQuery.Completed -= ProductsByStepQueryCompleted;
ProductsByStepQuery.Cancel();
}
if (string.IsNullOrEmpty(value))
{
ProductsByStepQuery = null;
}
else
{
ProductsByStepQuery =
(from p in _context.Products.AsAsyncQueryable()
let fullName = p.FullName
where fullName.Contains(value)
orderby fullName, p.Id
select new Product { Id = p.Id, FullName = fullName }).ToByStepQuery(10);
ProductsByStepQuery.Completed += ProductsByStepQueryCompleted;
ProductsByStepQuery.Load();
RefreshStopSearchCommandCanExecute();
}
}
}
private void ProductsByStepQueryCompleted()
{
ProductsByStepQuery.Completed -= ProductsByStepQueryCompleted;
RefreshStopSearchCommandCanExecute();
}
And the StopSearchCommand.
private RelayCommand _stopSearchCommand;
public ICommand StopSearchCommand
{
get
{
return _stopSearchCommand ??
(_stopSearchCommand = new RelayCommand(
() => ProductsByStepQuery.Cancel(),
() => ProductsByStepQuery != null && !ProductsByStepQuery.IsCompleted));
}
}
private void RefreshStopSearchCommandCanExecute()
{
if (_stopSearchCommand != null)
{
_stopSearchCommand.RaiseCanExecuteChanged();
}
}
And here you are.
You can find the solution here.