-
Notifications
You must be signed in to change notification settings - Fork 816
Expand file tree
/
Copy pathProgram.cs
More file actions
1144 lines (947 loc) · 47.8 KB
/
Copy pathProgram.cs
File metadata and controls
1144 lines (947 loc) · 47.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
namespace DocumentDB.Samples.Queries
{
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
//------------------------------------------------------------------------------------------------
// This sample demonstrates the use of LINQ and SQL Query Grammar to query DocumentDB Service
// For additional examples using the SQL query grammer refer to the SQL Query Tutorial available
// at https://azure.microsoft.com/documentation/articles/documentdb-sql-query/.
// There is also an interactive Query Demo web application where you can try out different
// SQL queries available at https://www.documentdb.com/sql/demo.
//------------------------------------------------------------------------------------------------
public class Program
{
private static DocumentClient client;
// Assign an id for your database & collection
private static readonly string DatabaseName = "samples";
private static readonly string CollectionName = "query-samples";
// Read the DocumentDB endpointUrl and authorizationKeys from config
// These values are available from the Azure Management Portal on the DocumentDB Account Blade under "Keys"
// NB > Keep these values in a safe & secure location. Together they provide Administrative access to your DocDB account
private static readonly string endpointUrl = ConfigurationManager.AppSettings["EndPointUrl"];
private static readonly string authorizationKey = ConfigurationManager.AppSettings["AuthorizationKey"];
// Set to true for this sample since it deals with different kinds of queries.
private static readonly FeedOptions DefaultOptions = new FeedOptions { EnableCrossPartitionQuery = true };
public static async Task Main(string[] args)
{
try
{
// DocumentClient should be a singleton
using (client = new DocumentClient(new Uri(endpointUrl), authorizationKey,
new ConnectionPolicy { ConnectionMode = ConnectionMode.Direct, ConnectionProtocol = Protocol.Tcp }))
{
await RunDemoAsync(DatabaseName, CollectionName);
}
}
#if !DEBUG
catch (Exception e)
{
LogException(e);
}
#endif
finally
{
Console.WriteLine("End of demo, press any key to exit.");
Console.ReadKey();
}
}
private static async Task RunDemoAsync(string databaseId, string collectionId)
{
Database database = await client.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseId });
DocumentCollection collection = await GetOrCreateCollectionAsync(databaseId, collectionId);
Uri collectionUri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);
await CreateDocuments(collectionUri);
//--------------------------------------------------------------------------------------------------------
// There are three ways of writing queries in the .NET SDK for DocumentDB,
// using the SQL Query Grammar, using LINQ Provider with Query and with Lambda.
// This sample will show each query using all methods.
// It is entirely up to you which style of query you write as they result in exactly the same query being
// executed on the service.
//
// There are some occasions when one syntax has advantages over others, but it's your choice which to use when
//--------------------------------------------------------------------------------------------------------
// Querying for all documents
await QueryAllDocumentsAsync(collectionUri);
// Querying for equality using ==
await QueryWithEquality(collectionUri);
// Querying for inequality using != and NOT
await QueryWithInequality(collectionUri);
// Querying using range operators like >, <, >=, <=
await QueryWithRangeOperatorsOnNumbers(collectionUri);
// Querying using range operators against strings. Needs a different indexing policy or the EnableScanInQuery directive.
await QueryWithRangeOperatorsOnStrings(collectionUri);
await QueryWithRangeOperatorsDateTimes(collectionUri);
// Querying with order by
await QueryWithOrderBy(collectionUri);
// Query with aggregate operators - Sum, Min, Max, Average, and Count
await QueryWithAggregates(collectionUri);
// Work with subdocuments
await QueryWithSubdocuments(collectionUri);
// Query with Intra-document Joins
await QueryWithJoins(collectionUri);
// Query with string, math and array operators
await QueryWithStringMathAndArrayOperators(collectionUri);
// Query with parameterized SQL using SqlQuerySpec
await QueryWithSqlQuerySpec(collectionUri);
// Query with explict Paging
await QueryWithPagingAsync(collectionUri);
// Query across multiple partitions in parallel
await QueryPartitionedCollectionInParallelAsync(collectionUri);
// Query using order by across multiple partitions
await QueryWithOrderByForPartitionedCollectionAsync(collectionUri);
// Uncomment to Cleanup
// await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId));
}
private static async Task QueryAllDocumentsAsync(Uri collectionUri)
{
// LINQ Query
var families =
from f in client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
select f;
int count = await families.CountAsync();
Assert("Expected two families", count == 2);
// LINQ Lambda
IQueryable<Family> allfamiliesQuery = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions);
IList<Family> allFamiles = await ExecuteQuery(allfamiliesQuery);
Assert("Expected two families", allFamiles.Count == 2);
// SQL
allfamiliesQuery = client.CreateDocumentQuery<Family>(
documentCollectionOrDatabaseUri: collectionUri,
sqlExpression: "SELECT * FROM Families",
feedOptions: DefaultOptions);
allFamiles = await ExecuteQuery(allfamiliesQuery);
Assert("Expected two families", allFamiles.Count == 2);
}
private static async Task QueryWithSqlQuerySpec(Uri collectionUri)
{
// Simple query with a single property equality comparison
// in SQL with SQL parameterization instead of inlining the
// parameter values in the query string
// LINQ Query -- Id == "value"
var query = client.CreateDocumentQuery<Family>(collectionUri, new SqlQuerySpec()
{
QueryText = "SELECT * FROM Families f WHERE (f.id = @id)",
Parameters = new SqlParameterCollection()
{
new SqlParameter("@id", "AndersenFamily")
}
}, DefaultOptions);
IList<Family> families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
// Query using two properties within each document. WHERE Id == "" AND Address.City == ""
// notice here how we are doing an equality comparison on the string value of City
query = client.CreateDocumentQuery<Family>(
collectionUri,
new SqlQuerySpec()
{
QueryText = "SELECT * FROM Families f WHERE f.id = @id AND f.Address.City = @city",
Parameters = new SqlParameterCollection()
{
new SqlParameter("@id", "AndersenFamily"),
new SqlParameter("@city", "Seattle")
}
}, DefaultOptions);
families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
}
private static async Task QueryWithEquality(Uri collectionUri)
{
// Simple query with a single property equality comparison
await QueryWithEqualsOnId(collectionUri);
// Query using two properties within each document (WHERE Id == "" AND Address.City == "")
// Notice here how we are doing an equality comparison on the string value of City
await QueryWithAndFilter(collectionUri);
//Query using a filter on two properties and include a custom projection
//in to a new anonymous type
await QueryWithAndFilterAndProjection(collectionUri);
}
private static async Task QueryWithAndFilterAndProjection(Uri collectionUri)
{
// LINQ Query -- Id == "value" OR City == "value"
var query =
(from f in client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
where f.Id == "AndersenFamily" || f.Address.City == "NY"
select new { Name = f.LastName, City = f.Address.City }).AsDocumentQuery();
while (query.HasMoreResults)
{
var items2 = await query.ExecuteNextAsync();
foreach (var item in items2)
{
Console.WriteLine("The {0} family live in {1}", item.Name, item.City);
}
}
var query2 = client.CreateDocumentQuery<Family>(
collectionUri,
new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = true })
.Where(d => d.LastName == "Andersen")
.Select(f => new { Name = f.LastName })
.AsDocumentQuery();
while (query2.HasMoreResults)
{
var items2 = await query2.ExecuteNextAsync();
foreach (var item in items2)
{
Console.WriteLine("The {0} family.", item.Name);
}
}
// LINQ Lambda -- Id == "value" OR City == "value"
var query3 = (client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.Where(f => f.Id == "AndersenFamily" || f.Address.City == "NY")
.Select(f => new { Name = f.LastName, City = f.Address.City })).AsDocumentQuery();
while (query3.HasMoreResults)
{
var items = await query3.ExecuteNextAsync();
foreach (var item in items)
{
Console.WriteLine("The {0} family live in {1}", item.Name, item.City);
}
}
// SQL -- Id == "value" OR City == "value"
var query4 = client.CreateDocumentQuery(collectionUri,
"SELECT f.LastName AS Name, f.Address.City AS City " +
"FROM Families f " +
"WHERE f.id='AndersenFamily' OR f.Address.City='NY'", DefaultOptions);
var items4 = await ExecuteQuery<dynamic>(query4);
foreach (var item in items4)
{
Console.WriteLine("The {0} family live in {1}", item.Name, item.City);
}
}
private static async Task QueryWithAndFilter(Uri collectionUri)
{
// LINQ Query
var query = from f in client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
where f.Id == "AndersenFamily" && f.Address.City == "Seattle"
select f;
var families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
// LINQ Lambda -- Id == "value" AND City == "value"
query = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.Where(f => f.Id == "AndersenFamily" && f.Address.City == "Seattle");
families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
// SQL -- Id == "value" AND City == "value"
query = client.CreateDocumentQuery<Family>(
collectionUri,
"SELECT * FROM Families f WHERE f.id='AndersenFamily' AND f.Address.City='Seattle'",
DefaultOptions);
families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
}
private static async Task QueryWithEqualsOnId(Uri collectionUri)
{
// LINQ Query -- Id == "value"
var familiesQuery =
from f in client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
where f.Id == "AndersenFamily"
select f;
var families = await ExecuteQuery(familiesQuery);
Assert("Expected only 1 family", families.Count == 1);
// LINQ Lambda -- Id == "value"
familiesQuery = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions).Where(f => f.Id == "AndersenFamily");
families = await ExecuteQuery(familiesQuery);
Assert("Expected only 1 family", families.Count == 1);
// SQL -- Id == "value"
familiesQuery = client.CreateDocumentQuery<Family>(
collectionUri,
"SELECT * FROM Families f WHERE f.id='AndersenFamily'",
DefaultOptions);
families = await ExecuteQuery(familiesQuery);
Assert("Expected only 1 family", families.Count == 1);
}
private static async Task QueryWithInequality(Uri collectionUri)
{
// Simple query with a single property inequality comparison
// LINQ Query
var query = from f in client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
where f.Id != "AndersenFamily"
select f;
var families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
// LINQ Lambda
query = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.Where(f => f.Id != "AndersenFamily");
families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
// SQL - in SQL you can use <> interchangably with != for "not equals"
query = client.CreateDocumentQuery<Family>(
collectionUri,
"SELECT * FROM Families f WHERE f.id <> 'AndersenFamily'",
DefaultOptions);
families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.ToList().Count == 1);
//combine equality and inequality
query =
from f in client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
where f.Id == "Wakefield" && f.Address.City != "NY"
select f;
families = await ExecuteQuery(query);
Assert("Expected no results", families.Count == 0);
query = client.CreateDocumentQuery<Family>(
collectionUri,
"SELECT * FROM Families f WHERE f.id = 'AndersenFamily' AND f.Address.City != 'NY'",
DefaultOptions);
families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
}
private static async Task QueryWithRangeOperatorsOnNumbers(Uri collectionUri)
{
// LINQ Query
var query = from f in client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
where f.Children[0].Grade > 5
select f;
var families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.ToList().Count == 1);
// LINQ Lambda
query = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.Where(f => f.Children[0].Grade > 5);
families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
// SQL
query = client.CreateDocumentQuery<Family>(collectionUri,
"SELECT * FROM Families f WHERE f.Children[0].Grade > 5",
DefaultOptions);
families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
}
private static async Task QueryWithRangeOperatorsOnStrings(Uri collectionUri)
{
// LINQ
var query = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.Where(f => f.Address.State.CompareTo("NY") > 0);
var families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
// SQL Query
query = client.CreateDocumentQuery<Family>(
collectionUri,
"SELECT * FROM Families f WHERE f.Address.State > 'NY'",
DefaultOptions);
families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
}
private static async Task QueryWithRangeOperatorsDateTimes(Uri collectionUri)
{
var query = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.Where(f => f.RegistrationDate >= DateTime.UtcNow.AddDays(-3));
var families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
query = client.CreateDocumentQuery<Family>(collectionUri,
string.Format("SELECT * FROM c WHERE c.RegistrationDate >= '{0}'",
DateTime.UtcNow.AddDays(-3).ToString("o")), DefaultOptions);
families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
}
private static async Task QueryWithOrderBy(Uri collectionUri)
{
// Order by with numbers. Works with default IndexingPolicy
await QueryWithOrderByNumbers(collectionUri);
// Order by with strings. Needs custom indexing policy. See GetOrCreateCollectionAsync
await QueryWithOrderByStrings(collectionUri);
}
private static async Task QueryWithOrderByNumbers(Uri collectionUri)
{
// LINQ Query
IQueryable<Family> query =
from f in client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
where f.LastName == "Andersen"
orderby f.Children[0].Grade
select f;
var families = await ExecuteQuery(query);
Assert("Expected 1 families", families.Count == 1);
// LINQ Lambda
query = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.Where(f => f.LastName == "Andersen")
.OrderBy(f => f.Children[0].Grade);
families = await ExecuteQuery(query);
Assert("Expected 1 families", families.Count == 1);
// SQL
query = client.CreateDocumentQuery<Family>(
collectionUri,
"SELECT * FROM Families f WHERE f.LastName = 'Andersen' ORDER BY f.Children[0].Grade",
DefaultOptions);
families = await ExecuteQuery(query);
Assert("Expected 1 families", families.Count == 1);
}
private static async Task QueryWithOrderByStrings(Uri collectionUri)
{
// LINQ Query
IQueryable<Family> query = from f in client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
where f.LastName == "Andersen"
orderby f.Address.State descending
select f;
var families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
// LINQ Lambda
query = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.Where(f => f.LastName == "Andersen")
.OrderByDescending(f => f.Address.State);
families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
// SQL
query = client.CreateDocumentQuery<Family>(
collectionUri,
"SELECT * FROM Families f WHERE f.LastName = 'Andersen' ORDER BY f.Address.State DESC",
DefaultOptions);
families = await ExecuteQuery(query);
Assert("Expected only 1 family", families.Count == 1);
}
private static async Task QueryWithAggregates(Uri collectionUri)
{
// SQL
var query = client.CreateDocumentQuery<int>(
collectionUri,
"SELECT VALUE COUNT(f) FROM Families f WHERE f.LastName = 'Andersen'",
DefaultOptions);
int count = (await ExecuteQuery(query)).First();
Assert("Expected only 1 family", count == 1);
// LINQ
count = await client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.Where(f => f.LastName == "Andersen")
.CountAsync();
Assert("Expected only 1 family", count == 1);
// SQL over an array within documents
query = client.CreateDocumentQuery<int>(
collectionUri,
"SELECT VALUE COUNT(child) FROM child IN f.Children",
DefaultOptions);
count = (await ExecuteQuery(query)).First();
Assert("Expected 3 children", count == 3);
// LINQ
count = await client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.SelectMany(f => f.Children)
.CountAsync();
Assert("Expected 3 children", count == 3);
// SQL over an array within documents
var maxGradeQuery = client.CreateDocumentQuery<int>(
collectionUri,
"SELECT VALUE MAX(child.Grade) FROM child IN f.Children",
DefaultOptions);
int maxGrade = (await ExecuteQuery(maxGradeQuery)).First();
Assert("Expected 8th grade", maxGrade == 8);
maxGrade = await client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.SelectMany(f => f.Children)
.Select(c => c.Grade)
.MaxAsync();
Assert("Expected 8th grade", maxGrade == 8);
}
private static async Task QueryWithSubdocuments(Uri collectionUri)
{
// DocumentDB supports the selection of sub-documents on the server, there
// is no need to send down the full family record if all you want to display
// is a single child
// SQL
var query = client.CreateDocumentQuery<Child>(
collectionUri,
"SELECT VALUE c FROM c IN f.Children",
DefaultOptions);
var childrenResult = await ExecuteQuery(query);
foreach (var child in childrenResult)
{
Console.WriteLine(JsonConvert.SerializeObject(child));
}
// LINQ Query
query = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.SelectMany(family => family.Children
.Select(c => c));
childrenResult = await ExecuteQuery(query);
foreach (var child in childrenResult)
{
Console.WriteLine(JsonConvert.SerializeObject(child));
}
}
private static async Task QueryWithJoins(Uri collectionUri)
{
// DocumentDB supports the notion of a Intradocument Join, or a self-join
// which will effectively flatten the hierarchy of a document, just like doing
// a self JOIN on a SQL table
// Below are three queries involving JOIN, shown in SQL and in LINQ, each produces the exact same result set
await QueryWithSingleJoin(collectionUri);
//now lets add a second level by joining the pets on to children which is joined to family
await QueryWithTwoJoins(collectionUri);
// Now let's add a filter to our JOIN query
await QueryWithTwoJoinsAndFilter(collectionUri);
}
private static async Task QueryWithTwoJoinsAndFilter(Uri collectionUri)
{
var query = client.CreateDocumentQuery<dynamic>(collectionUri,
"SELECT f.id as family, c.FirstName AS child, p.GivenName AS pet " +
"FROM Families f " +
"JOIN c IN f.Children " +
"JOIN p IN c.Pets " +
"WHERE p.GivenName = 'Fluffy'",
DefaultOptions);
var items = await ExecuteQuery(query);
foreach (var item in items)
{
Console.WriteLine(item);
}
// LINQ
var familiesChildrenAndPetsQuery = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.SelectMany(family => family.Children
.SelectMany(child => child.Pets
.Where(pet => pet.GivenName == "Fluffy")
.Select(pet => new
{
family = family.Id,
child = child.FirstName,
pet = pet.GivenName
}
))).AsDocumentQuery();
while (familiesChildrenAndPetsQuery.HasMoreResults)
{
var familiesChildrenAndPets = await familiesChildrenAndPetsQuery.ExecuteNextAsync();
foreach (var pet in familiesChildrenAndPets)
{
Console.WriteLine(pet);
}
}
}
private static async Task QueryWithTwoJoins(Uri collectionUri)
{
// SQL
var familiesChildrenAndPetsQuery = client.CreateDocumentQuery<dynamic>(
collectionUri,
"SELECT f.id as family, c.FirstName AS child, p.GivenName AS pet " +
"FROM Families f " +
"JOIN c IN f.Children " +
"JOIN p IN c.Pets ",
DefaultOptions);
var familiesChildrenAndPets = await ExecuteQuery(familiesChildrenAndPetsQuery);
foreach (var item in familiesChildrenAndPets)
{
Console.WriteLine(item);
}
// LINQ
var query = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.SelectMany(family => family.Children
.SelectMany(child => child.Pets
.Select(pet => new
{
family = family.Id,
child = child.FirstName,
pet = pet.GivenName
}
))).AsDocumentQuery();
while (query.HasMoreResults)
{
var items = await query.ExecuteNextAsync();
foreach (var item in items)
{
Console.WriteLine(item);
}
}
}
private static async Task QueryWithSingleJoin(Uri collectionUri)
{
// SQL
var query = client.CreateDocumentQuery(collectionUri,
"SELECT f.id " +
"FROM Families f " +
"JOIN c IN f.Children", DefaultOptions);
var items = await ExecuteQuery(query);
foreach (var item in items)
{
Console.WriteLine(JsonConvert.SerializeObject(item));
}
// LINQ
var familiesAndChildrenQuery = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.SelectMany(family => family.Children
.Select(c => family.Id));
var familiesAndChildren = await ExecuteQuery(familiesAndChildrenQuery);
foreach (var item in familiesAndChildren)
{
Console.WriteLine(JsonConvert.SerializeObject(item));
}
}
private static async Task QueryWithStringMathAndArrayOperators(Uri collectionUri)
{
// Find all families where the lastName starts with "An" -> should return the Andersens
IQueryable<Family> query = client.CreateDocumentQuery<Family>(
collectionUri,
"SELECT * FROM family WHERE STARTSWITH(family.LastName, 'An')",
DefaultOptions);
var results = await ExecuteQuery(query);
Assert("Expected only 1 family", results.Count == 1);
// Same query in LINQ. You can also use other operators like string.Contains(), string.EndsWith(), string.Trim(), etc.
query = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.Where(family => family.LastName.StartsWith("An"));
results = await ExecuteQuery(query);
Assert("Expected only 1 family", results.Count == 1);
// Round down numbers using FLOOR
IQueryable<int> numericQuery = client.CreateDocumentQuery<int>(
collectionUri,
"SELECT VALUE FLOOR(family.Children[0].Grade) FROM family",
DefaultOptions);
var numericResults = await ExecuteQuery(numericQuery);
Assert("Expected grades [5, 2]", numericResults.SequenceEqual(new[] { 5, 8 }));
// Same query in LINQ. You can also use other Math operators
numericQuery = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.Select(family => (int)Math.Round((double)family.Children[0].Grade));
numericResults = await ExecuteQuery(numericQuery);
Assert("Expected grades [5, 2]", numericResults.SequenceEqual(new[] { 5, 8 }));
// Get number of children using ARRAY_LENGTH
numericQuery = client.CreateDocumentQuery<int>(
collectionUri,
"SELECT VALUE ARRAY_LENGTH(family.Children) FROM family",
DefaultOptions);
numericResults = await ExecuteQuery(numericQuery);
Assert("Expected children count [1, 2]", numericResults.SequenceEqual(new[] { 1, 2 }));
// Same query in LINQ
numericQuery = client.CreateDocumentQuery<Family>(collectionUri, DefaultOptions)
.Select(family => family.Children.Count());
numericResults = await ExecuteQuery(numericQuery);
Assert("Expected children count [1, 2]", numericResults.SequenceEqual(new[] { 1, 2 }));
}
private static async Task QueryWithPagingAsync(Uri collectionUri)
{
// The .NET client automatically iterates through all the pages of query results
// Developers can explicitly control paging by creating an IDocumentQueryable
// using the IQueryable object, then by reading the ResponseContinuationToken values
// and passing them back as RequestContinuationToken in FeedOptions.
List<Family> families = new List<Family>();
// tell server we only want 1 record
FeedOptions options = new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = true };
// using AsDocumentQuery you get access to whether or not the query HasMoreResults
// If it does, just call ExecuteNextAsync until there are no more results
// No need to supply a continuation token here as the server keeps track of progress
var query = client.CreateDocumentQuery<Family>(collectionUri, options).AsDocumentQuery();
while (query.HasMoreResults)
{
foreach (Family family in await query.ExecuteNextAsync())
{
families.Add(family);
}
}
// The above sample works fine whilst in a loop as above, but
// what if you load a page of 1 record and then in a different
// Session at a later stage want to continue from where you were?
// well, now you need to capture the continuation token
// and use it on subsequent queries
query = client.CreateDocumentQuery<Family>(
collectionUri,
new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = true }).AsDocumentQuery();
var feedResponse = await query.ExecuteNextAsync<Family>();
string continuation = feedResponse.ResponseContinuation;
foreach (var f in feedResponse.AsEnumerable().OrderBy(f => f.Id))
{
if (f.Id != "AndersenFamily") throw new ApplicationException("Should only be the first family");
}
// Now the second time around use the contiuation token you got
// and start the process from that point
query = client.CreateDocumentQuery<Family>(
collectionUri,
new FeedOptions
{
MaxItemCount = 1,
RequestContinuation = continuation,
EnableCrossPartitionQuery = true
}).AsDocumentQuery();
feedResponse = await query.ExecuteNextAsync<Family>();
foreach (var f in feedResponse.OrderBy(f => f.Id))
{
if (f.Id != "WakefieldFamily") throw new ApplicationException("Should only be the second family");
}
}
private static async Task QueryPartitionedCollectionInParallelAsync(Uri collectionUri)
{
// The .NET client automatically iterates through all the pages of query results
// Developers can explicitly control paging by creating an IDocumentQueryable
// using the IQueryable object, then by reading the ResponseContinuationToken values
// and passing them back as RequestContinuationToken in FeedOptions.
List<Family> familiesSerial = new List<Family>();
String queryText = "SELECT * FROM Families";
// 0 maximum parallel tasks, effectively serial execution
FeedOptions options = new FeedOptions
{
MaxDegreeOfParallelism = 0,
MaxBufferedItemCount = 100,
EnableCrossPartitionQuery = true
};
var query = client.CreateDocumentQuery<Family>(collectionUri, queryText, options).AsDocumentQuery();
while (query.HasMoreResults)
{
FeedResponse<Family> response = await query.ExecuteNextAsync<Family>();
familiesSerial.AddRange(response);
}
Assert("Parallel Query expected two families", familiesSerial.Count == 2);
// 1 maximum parallel tasks, 1 dedicated asynchrousnous task to continuously make REST calls
List<Family> familiesParallel1 = new List<Family>();
options = new FeedOptions
{
MaxDegreeOfParallelism = 1,
MaxBufferedItemCount = 100,
EnableCrossPartitionQuery = true
};
query = client.CreateDocumentQuery<Family>(collectionUri, options).AsDocumentQuery();
while (query.HasMoreResults)
{
FeedResponse<Family> response = await query.ExecuteNextAsync<Family>();
familiesParallel1.AddRange(response);
}
Assert("Parallel Query expected two families", familiesParallel1.Count == 2);
AssertSequenceEqual("Parallel query returns result out of order compared to serial execution", familiesSerial, familiesParallel1);
// 10 maximum parallel tasks, a maximum of 10 dedicated asynchrousnous tasks to continuously make REST calls
List<Family> familiesParallel10 = new List<Family>();
options = new FeedOptions
{
MaxDegreeOfParallelism = 10,
MaxBufferedItemCount = 100,
EnableCrossPartitionQuery = true
};
query = client.CreateDocumentQuery<Family>(collectionUri, options).AsDocumentQuery();
while (query.HasMoreResults)
{
FeedResponse<Family> response = await query.ExecuteNextAsync<Family>();
familiesParallel10.AddRange(response);
}
Assert("Parallel Query expected two families", familiesParallel10.ToList().Count == 2);
AssertSequenceEqual("Parallel query returns result out of order compared to serial execution", familiesSerial, familiesParallel10);
}
private static async Task QueryWithOrderByForPartitionedCollectionAsync(Uri collectionUri)
{
// The .NET client automatically iterates through all the pages of query results
// Developers can explicitly control paging by creating an IDocumentQueryable
// using the IQueryable object, then by reading the ResponseContinuationToken values
// and passing them back as RequestContinuationToken in FeedOptions.
List<Family> familiesSerial = new List<Family>();
String queryText = "SELECT * FROM Families order by Families.LastName";
// 0 maximum parallel tasks, effectively serial execution
FeedOptions options = new FeedOptions
{
MaxDegreeOfParallelism = 0,
MaxBufferedItemCount = 100,
EnableCrossPartitionQuery = true
};
IDocumentQuery<Family> query = client.CreateDocumentQuery<Family>(collectionUri, queryText, options).AsDocumentQuery();
while (query.HasMoreResults)
{
FeedResponse<Family> response = await query.ExecuteNextAsync<Family>();
familiesSerial.AddRange(response);
}
Assert("Order By Query expected two families", familiesSerial.ToList().Count == 2);
// 1 maximum parallel tasks, 1 dedicated asynchrousnous task to continuously make REST calls
List<Family> familiesParallel1 = new List<Family>();
options = new FeedOptions
{
MaxDegreeOfParallelism = 1,
MaxBufferedItemCount = 100,
EnableCrossPartitionQuery = true
};
// using AsDocumentQuery you get access to whether or not the query HasMoreResults
// If it does, just call ExecuteNextAsync until there are no more results
// No need to supply a continuation token here as the server keeps track of progress
query = client.CreateDocumentQuery<Family>(collectionUri, queryText, options).AsDocumentQuery();
while (query.HasMoreResults)
{
FeedResponse<Family> response = await query.ExecuteNextAsync<Family>();
familiesParallel1.AddRange(response);
}
Assert("Order By Query expected two families", familiesParallel1.ToList().Count == 2);
AssertSequenceEqual("Parallel query returns result out of order compared to serial execution", familiesSerial, familiesParallel1);
// 10 maximum parallel tasks, a maximum of 10 dedicated asynchrousnous tasks to continuously make REST calls
List<Family> familiesParallel10 = new List<Family>();
options = new FeedOptions
{
MaxDegreeOfParallelism = 10,
MaxBufferedItemCount = 100,
EnableCrossPartitionQuery = true
};
query = client.CreateDocumentQuery<Family>(collectionUri, queryText, options).AsDocumentQuery();
while (query.HasMoreResults)
{
FeedResponse<Family> response = await query.ExecuteNextAsync<Family>();
familiesParallel10.AddRange(response);
}
Assert("Order By Query expected two families", familiesParallel10.ToList().Count == 2);
AssertSequenceEqual("Parallel query returns result out of order compared to serial execution", familiesSerial, familiesParallel10);
}
/// <summary>
/// Helper function to cast it from IQueryable to IDocumentQuery
/// </summary>
private static Task<IList<T>> ExecuteQuery<T>(IQueryable<T> query)
{
return ExecuteQuery<T>(query.AsDocumentQuery<T>());
}
/// <summary>
/// Helper function to fully drain the query in asynchonous way.
/// </summary>
/// <remarks>
/// Do NOT use ToList() and/or AsEnumerable(). These are blocking calls which
/// can lead to dead locks and latency issues.
/// </remarks>
/// <returns></returns>
private static async Task<IList<T>> ExecuteQuery<T>(IDocumentQuery<T> query)
{
using (query)
{
List<T> results = new List<T>();
while (query.HasMoreResults)
{
FeedResponse<T> queryResult = await query.ExecuteNextAsync<T>();
results.AddRange(queryResult);
}
return results;
}
}
/// <summary>
/// Creates the documents used in this Sample
/// </summary>
/// <param name="collectionUri">The selfLink property for the DocumentCollection where documents will be created.</param>
/// <returns>None</returns>
private static async Task CreateDocuments(Uri collectionUri)
{
Family AndersonFamily = new Family
{
Id = "AndersenFamily",
LastName = "Andersen",
Parents = new Parent[]
{
new Parent { FirstName = "Thomas" },
new Parent { FirstName = "Mary Kay"}
},
Children = new Child[]
{
new Child
{
FirstName = "Henriette Thaulow",
Gender = "female",
Grade = 5,
Pets = new []
{
new Pet { GivenName = "Fluffy" }
}
}
},
Address = new Address { State = "WA", County = "King", City = "Seattle" },
IsRegistered = true,