Пример #1
0
        //   Implemented "Filters Class" that builds filters and gives options of combining filter options Bin and Year.
        //        See "Using the MongoDB C# v2 Driver" on Pluralsight by Wes Higbee.  Module 2 Combining Filter Conditions
        public async Task <IEnumerable <Wine> > GetWinesAsync(WinesFilter filters)
        {
            var filterDefinition = filters.ToFilterDefinition();
            var sortDefinition   = filters.ToSortDefinition();

            var findOptions = new FindOptions <Wine, Wine>()
            {
                Sort = sortDefinition
            };

            var wines = await _wines.FindAsync(filterDefinition, findOptions).ConfigureAwait(false);

            return(await wines.ToListAsync().ConfigureAwait(false));
        }
Пример #2
0
        public async Task <IEnumerable <Wine> > GetWinesAsync(WinesFilter filters)
        {
            var filterDefinition = filters.ToFilterDefinition();

            // Get a BsonDocumentSerializer needed by the Render function
            var wineSerializer     = BsonSerializer.SerializerRegistry.GetSerializer <Wine>();
            var serializerRegistry = new BsonSerializerRegistry();

            try
            {
                var filterBsonDocument = filterDefinition.Render(wineSerializer, serializerRegistry);
                var year = filterBsonDocument?.Elements?.ToList().FirstOrDefault(e => e.Name == "year").Value?.AsInt32;
                var bin  = filterBsonDocument?.Elements?.ToList().FirstOrDefault(e => e.Name == "bin").Value?.AsInt32;

                var vineyard = filterBsonDocument?.Elements?.ToList().FirstOrDefault(e => e.Name == "vineyard").Value?.AsString;

                if (!string.IsNullOrEmpty(vineyard))
                {
                    await GetWinesByVineyardAsync(vineyard).ConfigureAwait(false);
                }


                if (year > 0)
                {
                    _wineList = _wineList.Where(w => w.Year == year)?.ToList();
                }

                if (bin > 0)
                {
                    _wineList = _wineList.Where(w => w.Bin == bin)?.ToList();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.Message},   InnerExec Message: {ex.InnerException?.Message}");
            }
            return(await Task.FromResult(_wineList).ConfigureAwait(false));
        }