Esempio n. 1
0
        public void ShouldReturnLastModifiedInUtc()
        {
            using (var store = GetDocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    session.Store(new User
                    {
                        Name = "Grisha"
                    });
                    session.SaveChanges();
                }

                using (var session = store.OpenSession())
                {
                    var query = from u in session.Query <User>()
                                select new
                    {
                        Name     = u.Name,
                        Metadata = RavenQuery.Metadata(u),
                    };

                    var list = query.ToList();

                    Assert.Equal(1, list.Count);

                    var lastModifiedFromProjection = list[0].Metadata.GetString(Constants.Documents.Metadata.LastModified);
                    Assert.EndsWith("Z", lastModifiedFromProjection);
                }
            }
        }
Esempio n. 2
0
        public void ShouldReturnLastModifiedInUtc()
        {
            using (var store = GetDocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    session.Store(new User
                    {
                        Name = "Grisha"
                    });
                    session.SaveChanges();
                }

                using (var session = store.OpenSession())
                {
                    var query = from u in session.Query <User>()
                                select new
                    {
                        Name         = u.Name,
                        Metadata     = RavenQuery.Metadata(u),
                        LastModified = RavenQuery.LastModified(u),
                    };

                    var list = query.ToList();

                    Assert.Equal(1, list.Count);

                    var lastModifiedFromProjection = list[0].Metadata.GetString(Constants.Documents.Metadata.LastModified);
                    Assert.Equal(DateTimeKind.Utc, list[0].LastModified.Kind);
                    DateTime dateTime = DateTime.ParseExact(lastModifiedFromProjection, "o", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
                    Assert.Equal(list[0].LastModified, dateTime.ToUniversalTime());
                }
            }
        }
Esempio n. 3
0
        public void CanSelectMetadata_SingleCallExpression()
        {
            using (var store = GetDocumentStore())
            {
                const string id = "users/1";

                using (var session = store.OpenSession())
                {
                    session.Store(new User(), id);
                    session.SaveChanges();
                }

                DateTime?lastModified;
                using (var session = store.OpenSession())
                {
                    var user = session.Load <User>(id);
                    lastModified = session.Advanced.GetLastModifiedFor(user);
                }

                using (var session = store.OpenSession())
                {
                    var query = session.Query <User>()
                                .Select(u => RavenQuery.Metadata(u));
                    var metadata = query.First();

                    Assert.True(metadata.TryGetValue(Constants.Documents.Metadata.LastModified, out string lm));
                    DateTime lastModifiedFromProjection = DateTime.ParseExact(lm, "o", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal).ToUniversalTime();
                    Assert.Equal(lastModified, lastModifiedFromProjection);
                }
            }
        }
Esempio n. 4
0
        public async Task <IActionResult> Index(Page currentPage, Home model)
        {
            using (var session = _documentStore.OpenAsyncSession()) {
                var postfix = $"/en";
                var query   = from page in session.Query <Page>()
                              where page.Id.In(new[] { "pages/33-A" })
                              let localizedDocument = RavenQuery.Load <Page>(page.Id + postfix)
                                                      let metadata = RavenQuery.Metadata(page)
                                                                     select new
                {
                    Id       = page.Id,
                    Name     = localizedDocument.Name,
                    Metadata = metadata
                };

                var result = await query.ToListAsync();
            }
            return(View());
        }
Esempio n. 5
0
        public async Task Examples()
        {
            using (var store = new DocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    #region projections_1
                    // request Name, City and Country for all entities from 'Companies' collection
                    var results = session
                                  .Query <Company>()
                                  .Select(x => new
                    {
                        Name    = x.Name,
                        City    = x.Address.City,
                        Country = x.Address.Country
                    })
                                  .ToList();
                    #endregion
                }

                using (var asyncSession = store.OpenAsyncSession())
                {
                    #region projections_1_async
                    // request Name, City and Country for all entities from 'Companies' collection
                    var results = await asyncSession
                                  .Query <Company>()
                                  .Select(x => new
                    {
                        Name    = x.Name,
                        City    = x.Address.City,
                        Country = x.Address.Country
                    })
                                  .ToListAsync();

                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_2
                    var results = session
                                  .Query <Order>()
                                  .Select(x => new
                    {
                        ShipTo   = x.ShipTo,
                        Products = x.Lines.Select(y => y.ProductName),
                    })
                                  .ToList();
                    #endregion
                }

                using (var asyncSession = store.OpenAsyncSession())
                {
                    #region projections_2_async
                    var results = await asyncSession
                                  .Query <Order>()
                                  .Select(x => new
                    {
                        ShipTo   = x.ShipTo,
                        Products = x.Lines.Select(y => y.ProductName),
                    })
                                  .ToListAsync();

                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_3
                    var results = (from e in session.Query <Employee>()
                                   select new
                    {
                        FullName = e.FirstName + " " + e.LastName,
                    }).ToList();
                    #endregion
                }

                using (var asyncSession = store.OpenAsyncSession())
                {
                    #region projections_3_async
                    var results = await(from e in asyncSession.Query <Employee>()
                                        select new
                    {
                        FullName = e.FirstName + " " + e.LastName,
                    }).ToListAsync();
                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_4
                    var results = session
                                  .Query <Order>()
                                  .Select(x => new
                    {
                        Total = x.Lines.Sum(l => l.PricePerUnit * l.Quantity),
                    })
                                  .ToList();
                    #endregion
                }

                using (var asyncSession = store.OpenAsyncSession())
                {
                    #region projections_4_async
                    var results = await asyncSession
                                  .Query <Order>()
                                  .Select(x => new
                    {
                        Total = x.Lines.Sum(l => l.PricePerUnit * l.Quantity),
                    })
                                  .ToListAsync();

                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_count_in_projection
                    var results = (from o in session.Query <Order>()
                                   let c = RavenQuery.Load <Company>(o.Company)
                                           select new
                    {
                        CompanyName = c.Name,
                        ShippedAt = o.ShippedAt,
                        TotalProducts = o.Lines.Count(),     //both empty syntax and with a predicate is supported
                        TotalDiscountedProducts = o.Lines.Count(x => x.Discount > 0)
                    }).ToList();
                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_5
                    var results = (from o in session.Query <Order>()
                                   let c = RavenQuery.Load <Company>(o.Company)
                                           select new
                    {
                        CompanyName = c.Name,
                        ShippedAt = o.ShippedAt
                    }).ToList();
                    #endregion
                }

                using (var asyncSession = store.OpenAsyncSession())
                {
                    #region projections_5_async
                    var results = (from o in asyncSession.Query <Order>()
                                   let c = RavenQuery.Load <Company>(o.Company)
                                           select new
                    {
                        CompanyName = c.Name,
                        ShippedAt = o.ShippedAt
                    }).ToListAsync();
                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_6
                    var results = session
                                  .Query <Employee>()
                                  .Select(e => new
                    {
                        DayOfBirth   = e.Birthday.Day,
                        MonthOfBirth = e.Birthday.Month,
                        Age          = DateTime.Today.Year - e.Birthday.Year
                    })
                                  .ToList();
                    #endregion
                }

                using (var asyncSession = store.OpenAsyncSession())
                {
                    #region projections_6_async
                    var results = await asyncSession
                                  .Query <Employee>()
                                  .Select(e => new
                    {
                        DayOfBirth   = e.Birthday.Day,
                        MonthOfBirth = e.Birthday.Month,
                        Age          = DateTime.Today.Year - e.Birthday.Year
                    })
                                  .ToListAsync();

                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_7
                    var results = from e in session.Query <Employee>()
                                  select new
                    {
                        Date = RavenQuery.Raw <DateTime>("new Date(Date.parse(e.Birthday))"),
                        Name = RavenQuery.Raw(e.FirstName, "substr(0,3)"),
                    };
                    #endregion
                }

                using (var asyncSession = store.OpenAsyncSession())
                {
                    #region projections_7_async
                    var results = await(from e in asyncSession.Query <Employee>()
                                        select new
                    {
                        Date = RavenQuery.Raw <DateTime>("new Date(Date.parse(e.Birthday))"),
                        Name = RavenQuery.Raw(e.FirstName, "substr(0,3)"),
                    }).ToListAsync();
                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_8
                    var results = session.Query <Company, Companies_ByContact>()
                                  .ProjectInto <ContactDetails>()
                                  .ToList();
                    #endregion
                }

                using (var asyncSession = store.OpenAsyncSession())
                {
                    #region projections_8_async
                    var results = await asyncSession.Query <Company, Companies_ByContact>()
                                  .ProjectInto <ContactDetails>()
                                  .ToListAsync();

                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_10
                    // query index 'Products_BySupplierName'
                    // return documents from collection 'Products' that have a supplier 'Norske Meierier'
                    // project them to 'Products'
                    List <Product> results = session
                                             .Query <Products_BySupplierName.Result, Products_BySupplierName>()
                                             .Where(x => x.Name == "Norske Meierier")
                                             .OfType <Product>()
                                             .ToList();
                    #endregion
                }

                using (var asyncSession = store.OpenAsyncSession())
                {
                    #region projections_10_async
                    // query index 'Products_BySupplierName'
                    // return documents from collection 'Products' that have a supplier 'Norske Meierier'
                    // project them to 'Products'
                    List <Product> results = await asyncSession
                                             .Query <Products_BySupplierName.Result, Products_BySupplierName>()
                                             .Where(x => x.Name == "Norske Meierier")
                                             .OfType <Product>()
                                             .ToListAsync();

                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_12
                    var results = (from e in session.Query <Employee>()
                                   let format = (Func <Employee, string>)(p => p.FirstName + " " + p.LastName)
                                                select new
                    {
                        FullName = format(e)
                    }).ToList();
                    #endregion
                }

                using (var asyncSession = store.OpenAsyncSession())
                {
                    #region projections_12_async
                    var results = await(from e in asyncSession.Query <Employee>()
                                        let format = (Func <Employee, string>)(p => p.FirstName + " " + p.LastName)
                                                     select new
                    {
                        FullName = format(e)
                    }).ToListAsync();
                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_13
                    var results = (from e in session.Query <Employee>()
                                   select new
                    {
                        Name = e.FirstName,
                        Metadata = RavenQuery.Metadata(e),
                    }).ToList();
                    #endregion
                }

                using (var asyncSession = store.OpenAsyncSession())
                {
                    #region projections_13_async
                    var results = await(from e in asyncSession.Query <Employee>()
                                        select new
                    {
                        Name     = e.FirstName,
                        Metadata = RavenQuery.Metadata(e),
                    }).ToListAsync();
                    #endregion
                }
            }
        }
Esempio n. 6
0
        public void Sample()
        {
            using (var store = new DocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    #region projections_1
                    var results = session
                                  .Query <Employee, Employees_ByFirstAndLastName>()
                                  .Select(x => new
                    {
                        FirstName = x.FirstName,
                        LastName  = x.LastName
                    })
                                  .ToList();
                    #endregion
                }
                using (var session = store.OpenSession())
                {
                    #region projections_1_stored
                    var results = session
                                  .Query <Employee, Employees_ByFirstAndLastNameWithStoredFields>()
                                  .Select(x => new
                    {
                        FirstName = x.FirstName,
                        LastName  = x.LastName
                    })
                                  .ToList();
                    #endregion
                }
                using (var session = store.OpenSession())
                {
                    #region projections_2
                    var results = session
                                  .Query <Order, Orders_ByShipToAndLines>()
                                  .Select(x => new
                    {
                        ShipTo   = x.ShipTo,
                        Products = x.Lines.Select(y => y.ProductName),
                    })
                                  .ToList();
                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_3
                    var results = session
                                  .Query <Employee, Employees_ByFirstAndLastName>()
                                  .Select(x => new
                    {
                        FullName = x.FirstName + " " + x.LastName
                    })
                                  .ToList();
                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_4
                    var results = (from e in session.Query <Employee, Employees_ByFirstAndLastName>()
                                   let format = (Func <Employee, string>)(p => p.FirstName + " " + p.LastName)
                                                select new
                    {
                        FullName = format(e)
                    }).ToList();
                    #endregion
                }
                using (var session = store.OpenSession())
                {
                    #region projections_5
                    var results = (from o in session.Query <Order, Orders_ByShippedAtAndCompany>()
                                   let c = RavenQuery.Load <Company>(o.Company)
                                           select new
                    {
                        CompanyName = c.Name,
                        ShippedAt = o.ShippedAt
                    }).ToList();
                    #endregion
                }
                using (var session = store.OpenSession())
                {
                    #region projections_6
                    var results = session
                                  .Query <Employee, Employees_ByFirstNameAndBirthday>()
                                  .Select(e => new
                    {
                        DayOfBirth   = e.Birthday.Day,
                        MonthOfBirth = e.Birthday.Month,
                        Age          = DateTime.Today.Year - e.Birthday.Year
                    }).ToList();
                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_7
                    var results = session
                                  .Query <Employee, Employees_ByFirstNameAndBirthday>()
                                  .Select(e => new
                    {
                        Date = RavenQuery.Raw <DateTime>("new Date(Date.parse(e.Birthday))"),
                        Name = RavenQuery.Raw(e.FirstName, "substr(0,3)")
                    }).ToList();
                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_8
                    var results = session
                                  .Query <Employee, Employees_ByFirstAndLastName>()
                                  .Select(e => new
                    {
                        Name     = e.FirstName,
                        Metadata = RavenQuery.Metadata(e),
                    })
                                  .ToList();
                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_9
                    var results = session
                                  .Query <Order, Orders_ByShipToAndLines>()
                                  .Select(x => new
                    {
                        Total = x.Lines.Sum(l => l.PricePerUnit * l.Quantity)
                    })
                                  .ToList();
                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region selectfields_1
                    var fields = new string[] {
                        "Name",
                        "Phone"
                    };

                    var results = session
                                  .Advanced
                                  .DocumentQuery <Company, Companies_ByContact>()
                                  .SelectFields <ContactDetails>(fields)
                                  .ToList();
                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region selectfields_2
                    var results = session
                                  .Advanced
                                  .DocumentQuery <Company, Companies_ByContact>()
                                  .SelectFields <ContactDetails>()
                                  .ToList();
                    #endregion
                }

                using (var session = store.OpenSession())
                {
                    #region projections_10
                    var results = session.Query <Company, Companies_ByContact>()
                                  .ProjectInto <ContactDetails>()
                                  .ToList();
                    #endregion
                }
            }
        }