示例#1
0
        public static Func <bool, IQueryable <Data> > GenerateSimpleData(
            DocumentClient client,
            Database testDb,
            out DocumentCollection testCollection)
        {
            const int DocumentCount = 10;
            PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition {
                Paths = new System.Collections.ObjectModel.Collection <string>(new[] { "/Id" }), Kind = PartitionKind.Hash
            };

            testCollection = client.CreateDocumentCollectionAsync(
                testDb.GetLink(),
                new DocumentCollection()
            {
                Id = Guid.NewGuid().ToString(), PartitionKey = partitionKeyDefinition
            }).Result;

            int    seed   = DateTime.Now.Millisecond;
            Random random = new Random(seed);

            Debug.WriteLine("Random seed: {0}", seed);
            List <Data> testData = new List <Data>();

            for (int index = 0; index < DocumentCount; index++)
            {
                Data dataEntry = new Data()
                {
                    Number    = random.Next(-10000, 10000),
                    Flag      = index % 2 == 0 ? true : false,
                    Multiples = new int[] { index, index * 2, index * 3, index * 4 }
                };

                client.CreateDocumentAsync(testCollection.GetLink(), dataEntry).Wait();
                testData.Add(dataEntry);
            }

            FeedOptions feedOptions = new FeedOptions()
            {
                EnableScanInQuery = true, EnableCrossPartitionQuery = true
            };
            var query = client.CreateDocumentQuery <Data>(testCollection.GetLink(), feedOptions).AsQueryable();

            // To cover both query against backend and queries on the original data using LINQ nicely,
            // the LINQ expression should be written once and they should be compiled and executed against the two sources.
            // That is done by using Func that take a boolean Func. The parameter of the Func indicate whether the Cosmos DB query
            // or the data list should be used. When a test is executed, the compiled LINQ expression would pass different values
            // to this getQuery method.
            IQueryable <Data> getQuery(bool useQuery) => useQuery ? query : testData.AsQueryable();

            return(getQuery);
        }