コード例 #1
0
        public void Effort_FilterByDto()
        {
            using (var context = new TestDbContext(Effort.DbConnectionFactory.CreateTransient()))
            {
                IQueryable<OrderDto> sourceResult = new OrderDto[0]
                    .AsQueryable()
                    .Where(s => s.FullName.EndsWith("Bestellung"))
                    .Map<OrderDto, Order>(context.OrderSet)
                    .ProjectTo<OrderDto>(); // projection added

                var dtos = sourceResult.ToList();

                Assert.AreEqual(2, dtos.Count);
            }
        }
コード例 #2
0
        public void Effort_FilterByMappedQuery()
        {
            using (var context = new TestDbContext(Effort.DbConnectionFactory.CreateTransient()))
            {
                // works but requires filters (Where, ...) to be specified before call to "Map"
                // however, we'd like to apply filters to the resulting IQueryable "sourceResult".
                // that does not work though.
                IQueryable<OrderDto> sourceResult = new OrderDto[0]
                    .AsQueryable()
                    .Where(s => s.FullName.EndsWith("Bestellung"))
                    .Map<OrderDto, Order>(context.OrderSet)
                    .ProjectTo<OrderDto>(); // projection added
                var dtos = sourceResult.ToList();
                Assert.AreEqual(2, dtos.Count);

                // this is what we try to achieve but it does not work
                // as the mapping is done right away by "Map<>" and "ProjectTo<>"
                // and the .Where() filter does not at that time
                // so it is not mapped and results in a
                // "System.NotSupportedException : The specified type member 'FullName' is not supported 
                // ...in LINQ to Entities. Only initializers, entity members, and entity navigation properties 
                // ...are supported."
                try
                {
                    IQueryable<OrderDto> sourceResult2 = new OrderDto[0]
                        .AsQueryable()
                        .Map<OrderDto, Order>(context.OrderSet)
                        .ProjectTo<OrderDto>(); // projection added
                    var dtos2 = sourceResult
                        .Where(s => s.FullName.EndsWith("Bestellung"))
                        .ToList();

                    //Assert.Fail("NotSupportedException was expected");
                }
                catch (NotSupportedException)
                {
                }
                
                // Using "AsDataSource"
                IQueryable<OrderDto> sourceResult4 = context.OrderSet.UseAsDataSource(Mapper.Configuration).For<OrderDto>();
                var dtos4 = sourceResult4.Where(d => d.FullName.EndsWith("Bestellung")).ToList();

                Assert.AreEqual(2, dtos4.Count);
            }
        }