public void GroupCollection(IEnumerable <Produto> produtos, IEnumerable <ProdutoListaCompra> produtoListaCompra = null) { ProdutoListaCompraViewModel[] produtosListaCompraViewModel = null; if (produtoListaCompra == null) { produtosListaCompraViewModel = produtos.Select(x => new ProdutoListaCompraViewModel(azureService, this, listaCompraService, x)).ToArray(); } else { produtosListaCompraViewModel = produtoListaCompra.Select(x => new ProdutoListaCompraViewModel(azureService, this, listaCompraService, x)).ToArray(); } var produtosListaCompra = new ObservableCollection <GroupCollection <char, ProdutoListaCompraViewModel> >(from e in produtosListaCompraViewModel orderby e.ProdutoListaCompra?.Produto?.Nome group e by e.ProdutoListaCompra?.Produto?.Nome[0] into grupos select new GroupCollection <char, ProdutoListaCompraViewModel>(grupos.Key.GetValueOrDefault(), grupos)); foreach (var item in produtosListaCompra) { ProdutosListaCompra.Add(item); } }
private async void ExecuteSearchProductAsync(string expression) { if (expression == null) { return; } if (expression?.Length == 1 && CachedList.Count == 0) { IsNotSearching = false; OnPropertyChanged(nameof(IsNotSearching)); CachedList = new ObservableCollection <GroupCollection <char, ProdutoListaCompraViewModel> >(ProdutosListaCompra); } ProdutosListaCompra.Clear(); if (string.IsNullOrEmpty(expression)) { IsNotSearching = true; OnPropertyChanged(nameof(IsNotSearching)); foreach (var item in CachedList) { ProdutosListaCompra.Add(item); } await listaCompraService.LoadAutoCompleteAsync(TransformGroupedCollectionToModel(ProdutosListaCompra).Select(x => x.Produto?.Id).ToArray()); CachedList.Clear(); return; } var produtos = await listaCompraService.ObterProdutoPorNome(expression); if (produtos != null) { GroupCollection(produtos); } }