public void TestNestedSelectMany() { DocumentClient client = TestCommon.CreateClient(true); IOrderedQueryable <Family> families = new DocumentQuery <Family>(client, ResourceType.Document, typeof(Document), null, null); IQueryable query = families.SelectMany(family => family.Children .SelectMany(child => child.Pets .Where(pet => pet.GivenName == "Fluffy") .Select(pet => pet ))); this.VerifyQueryTranslation(query.ToString(), "SELECT VALUE pet FROM root JOIN child IN root[\"Children\"] JOIN pet IN child[\"Pets\"] WHERE (pet[\"GivenName\"] = \"Fluffy\") "); query = families.SelectMany(family => family.Children .SelectMany(child => child.Pets .Where(pet => pet.GivenName == "Fluffy") .Select(pet => new { family = family.FamilyId, child = child.GivenName, pet = pet.GivenName } ))); this.VerifyQueryTranslation(query.ToString(), "SELECT VALUE {\"family\": root[\"id\"], \"child\": child[\"GivenName\"], \"pet\": pet[\"GivenName\"]} FROM root JOIN child IN root[\"Children\"] JOIN pet IN child[\"Pets\"] WHERE (pet[\"GivenName\"] = \"Fluffy\") "); }
public void ValidateDynamicDocumentQuery() //Ensure query on custom property of document. { DocumentClient client = TestCommon.CreateClient(true); Book myDocument = new Book(); myDocument.Id = Guid.NewGuid().ToString(); myDocument.Title = "My Book"; //Simple Property. myDocument.Languages = new Language[] { new Language { Name = "English", Copyright = "London Publication" }, new Language { Name = "French", Copyright = "Paris Publication" } }; //Array Property myDocument.Author = new Author { Name = "Don", Location = "France" }; //Complex Property myDocument.Price = 9.99; myDocument.Editions = new List <Edition>() { new Edition() { Name = "First", Year = 2001 }, new Edition() { Name = "Second", Year = 2005 } }; //Create second document to make sure we have atleast one document which are filtered out of query. Book secondDocument = new Book { Id = Guid.NewGuid().ToString(), Title = "My Second Book", Languages = new Language[] { new Language { Name = "Spanish", Copyright = "Mexico Publication" } }, Author = new Author { Name = "Carlos", Location = "Cancun" }, Price = 25, Editions = new List <Edition>() { new Edition() { Name = "First", Year = 1970 } } }; //Unfiltered execution. DocumentQuery <Book> bookDocQuery = new DocumentQuery <Book>(client, ResourceType.Document, typeof(Document), null, null); //Simple Equality on custom property. IQueryable <dynamic> docQuery = from book in bookDocQuery where book.Title == "My Book" select book; this.VerifyQueryTranslation(docQuery, "SELECT * FROM root WHERE (root[\"title\"] = \"My Book\") "); //Nested Property access docQuery = from book in bookDocQuery where book.Author.Name == "Don" select book; this.VerifyQueryTranslation(docQuery, "SELECT * FROM root WHERE (root[\"Author\"][\"id\"] = \"Don\") "); //Array references & Project Author out.. docQuery = from book in bookDocQuery where book.Languages[0].Name == "English" select book.Author; this.VerifyQueryTranslation(docQuery, "SELECT VALUE root[\"Author\"] FROM root WHERE (root[\"Languages\"][0][\"Name\"] = \"English\") "); //SelectMany docQuery = bookDocQuery.SelectMany( book => book.Languages).Where(lang => lang.Name == "French").Select(lang => lang.Copyright); this.VerifyQueryTranslation(docQuery, "SELECT VALUE tmp[\"Copyright\"] FROM root JOIN tmp IN root[\"Languages\"] WHERE (tmp[\"Name\"] = \"French\") "); //NumericRange query docQuery = from book in bookDocQuery where book.Price < 10 select book.Author; this.VerifyQueryTranslation(docQuery, "SELECT VALUE root[\"Author\"] FROM root WHERE (root[\"Price\"] < 10.0) "); //Or query docQuery = from book in bookDocQuery where book.Title == "My Book" || book.Author.Name == "Don" select book; this.VerifyQueryTranslation(docQuery, "SELECT * FROM root WHERE ((root[\"title\"] = \"My Book\") OR (root[\"Author\"][\"id\"] = \"Don\")) "); //SelectMany query on a List type. docQuery = bookDocQuery .SelectMany(book => book.Editions) .Select(ed => ed.Name); this.VerifyQueryTranslation(docQuery, "SELECT VALUE tmp[\"Name\"] FROM root JOIN tmp IN root[\"Editions\"] "); // Below samples are strictly speaking not Any equivalent. But they join and filter "all" // subchildren which match predicate. When SQL BE supports ANY, we can replace these with Any Flavor. docQuery = bookDocQuery .SelectMany(book => book.Languages .Where(lng => lng.Name == "English") .Select(lng => book.Author)); this.VerifyQueryTranslation(docQuery, "SELECT VALUE root[\"Author\"] FROM root JOIN lng IN root[\"Languages\"] WHERE (lng[\"Name\"] = \"English\") "); //Any query on a List type. docQuery = bookDocQuery .SelectMany(book => book.Editions .Where(edition => edition.Year == 2001) .Select(lng => book.Author)); this.VerifyQueryTranslation(docQuery, "SELECT VALUE root[\"Author\"] FROM root JOIN edition IN root[\"Editions\"] WHERE (edition[\"Year\"] = 2001) "); }