public void DocumentQuerySelectFields() { using (var store = GetDocumentStore()) { var definition = new IndexDefinitionBuilder <Order>("OrderByCompanyCountryIndex") { Map = docs => from doc in docs select new { doc.Company, ShipTo_Country = doc.ShipTo.Country } }.ToIndexDefinition(store.Conventions); store.Maintenance.Send(new PutIndexesOperation(definition)); using (var session = store.OpenSession()) { session.Store(new Order { Company = "companies/1", ShipTo = new Address() { Country = "Sweden", City = "Stockholm" } }); session.Store(new Order { Company = "companies/1", ShipTo = new Address() { Country = "Germany", City = "Berlin" } }); session.Store(new Company { Name = "HR" }, "companies/1"); session.SaveChanges(); } WaitForIndexing(store); using (var session = store.OpenSession()) { var docQuery = session.Advanced .DocumentQuery <Order>("OrderByCompanyCountryIndex") .WhereEquals(x => x.ShipTo.Country, "Sweden") .SelectFields <OrderResult>(QueryData.CustomFunction( alias: "o", func: "{ Order : o, Company : load(o.Company) }") ); Assert.Equal("from index 'OrderByCompanyCountryIndex' as o where ShipTo_Country = $p0 " + "select { Order : o, Company : load(o.Company) }", docQuery.ToString()); var result = docQuery.ToList(); Assert.Equal(1, result.Count); Assert.Equal("HR", result[0].Company.Name); } } }
public void CanUseConditionalLoadLazily() { using (var store = GetDocumentStore()) { using (var bulkInsert = store.BulkInsert()) { for (int i = 0; i < 100; i++) { bulkInsert.Store(new Company()); } } var ids = new List <Result>(); var loads = new List <Lazy <(Company Entity, string ChangeVector)> >(); using (var session1 = store.OpenSession()) { ids = session1.Advanced.DocumentQuery <Company>() .WaitForNonStaleResults() .SelectFields <Result>(QueryData.CustomFunction( alias: "o", func: "{ Id : id(o), ChangeVector : getMetadata(o)['@change-vector'] }") ).ToList(); session1.Load <Company>(ids.Select(x => x.Id).ToList()); var res = session1.Load <Company>(ids.Take(50).Select(x => x.Id).ToList()); var c = 0; foreach (var kvp in res) { kvp.Value.Phone = ++c; } session1.SaveChanges(); } using (var session = store.OpenSession()) { // load last 10 session.Load <Company>(ids.Skip(90).Take(10).Select(x => x.Id).ToList()); var numberOfRequestsPerSession = session.Advanced.NumberOfRequests; foreach (var res in ids) { loads.Add(session.Advanced.Lazily.ConditionalLoad <Company>(res.Id, res.ChangeVector)); } session.Advanced.Eagerly.ExecuteAllPendingLazyOperations(); Assert.Equal(numberOfRequestsPerSession + 1, session.Advanced.NumberOfRequests); for (int i = 0; i < 100; i++) { var l = loads[i]; Assert.False(l.IsValueCreated); (Company entity, string changeVector) = l.Value; if (i < 50) { // load from server Assert.Equal(ids[i].Id, entity.Id); Assert.NotEqual(ids[i].ChangeVector, changeVector); } else if (i < 90) { // mot modified Assert.Null(entity); Assert.Equal(ids[i].ChangeVector, changeVector); } else { // tracked in the session Assert.Equal(ids[i].Id, entity.Id); Assert.NotNull(entity); Assert.Equal(ids[i].ChangeVector, changeVector); } // not exist on server var lazy = session.Advanced.Lazily.ConditionalLoad <Company>("Companies/322-A", ids[0].ChangeVector); var load = lazy.Value; Assert.Null(load.Entity); Assert.Null(load.ChangeVector); } } } }