private static MobileServiceTableQuery <Movie> ApplyOrdering(MobileServiceTableQuery <Movie> query, OrderByClause[] orderBy) { if (orderBy.Length == 1) { if (orderBy[0].IsAscending && orderBy[0].FieldName == "Title") { return(query.OrderBy(m => m.Title)); } else if (!orderBy[0].IsAscending && orderBy[0].FieldName == "Year") { return(query.OrderByDescending(m => m.Year)); } } else if (orderBy.Length == 2) { if (orderBy[1].FieldName == "Title" && orderBy[1].IsAscending) { if (orderBy[0].FieldName == "Duration" && orderBy[0].IsAscending) { return(query.OrderBy(m => m.Duration).ThenBy(m => m.Title)); } else if (orderBy[0].FieldName == "ReleaseDate" && !orderBy[0].IsAscending) { return(query.OrderByDescending(m => m.ReleaseDate).ThenBy(m => m.Title)); } } } throw new NotImplementedException(string.Format("Ordering by [{0}] not implemented yet", string.Join(", ", orderBy.Select(c => string.Format("{0} {1}", c.FieldName, c.IsAscending ? "asc" : "desc"))))); }
public InviteUserViewModel(MainViewModel parent, IMobileServiceTable <Profile> profileTable, Action dismiss) { _profileTable = profileTable; _parent = parent; _dismiss = dismiss; SearchCommand = new DelegateCommand(() => { _currentPage = 0; if (String.IsNullOrWhiteSpace(SearchText)) { _query = profileTable.Take(_pageCount).IncludeTotalCount(); } else { _query = profileTable.Take(_pageCount).IncludeTotalCount().Where(p => p.Name.IndexOf(SearchText) > -1); } RefreshQuery(); }); NextCommand = new DelegateCommand(() => { _currentPage++; RefreshQuery(); }, false); PrevCommand = new DelegateCommand(() => { _currentPage--; RefreshQuery(); }, false); }
public InviteUserViewModel(MainViewModel parent, IMobileServiceTable<Profile> profileTable, Action dismiss) { _profileTable = profileTable; _parent = parent; _dismiss = dismiss; SearchCommand = new DelegateCommand(() => { _currentPage = 0; if (String.IsNullOrWhiteSpace(SearchText)) { _query = profileTable.Take(_pageCount).IncludeTotalCount(); } else { _query = profileTable.Take(_pageCount).IncludeTotalCount().Where(p => p.Name.IndexOf(SearchText) > -1); } RefreshQuery(); }); NextCommand = new DelegateCommand(() => { _currentPage++; RefreshQuery(); }, false); PrevCommand = new DelegateCommand(() => { _currentPage--; RefreshQuery(); }, false); }
public async Task TotalCountBasics() { // Insert a few records IMobileServiceTable <ToDo> table = GetClient().GetTable <ToDo>(); ToDo first = new ToDo { Title = "ABC", Complete = false }; await table.InsertAsync(first); await table.InsertAsync(new ToDo { Title = "DEF", Complete = true }); await table.InsertAsync(new ToDo { Title = "GHI", Complete = false }); ITotalCountProvider countProvider = null; MobileServiceTableQuery <ToDo> query = table.Where(t => t.Id >= first.Id); // Run a simple query and verify we get all 3 items, but the // TotalCount is not provided. List <ToDo> items = await query.ToListAsync(); countProvider = items as ITotalCountProvider; Assert.AreEqual(3, items.Count); Assert.IsNotNull(countProvider); Assert.AreEqual(-1L, countProvider.TotalCount); IEnumerable <ToDo> sequence = await query.ToEnumerableAsync(); countProvider = sequence as ITotalCountProvider; Assert.IsNotNull(countProvider); Assert.AreEqual(-1L, countProvider.TotalCount); // Now use IncludeTotalCount and make sure we get the expected // number of results query = query.IncludeTotalCount(); items = await query.ToListAsync(); countProvider = items as ITotalCountProvider; Assert.AreEqual(3, items.Count); Assert.IsNotNull(countProvider); Assert.AreEqual(3L, countProvider.TotalCount); sequence = await query.ToEnumerableAsync(); countProvider = sequence as ITotalCountProvider; Assert.IsNotNull(countProvider); Assert.AreEqual(3L, countProvider.TotalCount); // Verify that IncludeTotalCount is correctly propagated with // projections List <string> titles = await query.Select(t => t.Title).ToListAsync(); countProvider = titles as ITotalCountProvider; Assert.AreEqual(3, titles.Count); Assert.IsNotNull(countProvider); Assert.AreEqual(3L, countProvider.TotalCount); }
private static MobileServiceTableQueryDescription Compile <T, U>(Func <IMobileServiceTable <T>, MobileServiceTableQuery <U> > getQuery) { MobileServiceClient service = new MobileServiceClient("http://www.test.com"); IMobileServiceTable <T> table = service.GetTable <T>(); MobileServiceTableQuery <U> query = getQuery(table); MobileServiceTableQueryDescription odata = query.Compile(); App.Harness.Log(">>> " + odata.ToString()); return(odata); }
private static ZumoTest CreateQueryTest <TExpectedException>( string name, Expression <Func <Movie, bool> > whereClause, int?top = null, int?skip = null, OrderByClause[] orderBy = null, Expression <Func <Movie, string> > selectExpression = null, bool?includeTotalCount = null) where TExpectedException : Exception { return(new ZumoTest(name, async delegate(ZumoTest test) { try { var table = ZumoTestGlobals.Instance.Client.GetTable <Movie>(); MobileServiceTableQuery <Movie> query = null; MobileServiceTableQuery <string> selectedQuery = null; if (whereClause != null) { query = table.Where(whereClause); } if (orderBy != null) { if (query == null) { query = table.Where(m => m.Duration == m.Duration); } query = ApplyOrdering(query, orderBy); } if (top.HasValue) { query = query == null ? table.Take(top.Value) : query.Take(top.Value); } if (skip.HasValue) { query = query == null ? table.Skip(skip.Value) : query.Skip(skip.Value); } if (selectExpression != null) { selectedQuery = query == null ? table.Select(selectExpression) : query.Select(selectExpression); } if (includeTotalCount.HasValue) { query = query.IncludeTotalCount(); } IEnumerable <Movie> readMovies = null; IEnumerable <string> readProjectedMovies = null; if (selectedQuery == null) { // Both ways of querying should be equivalent, so using both with equal probability here. var tickCount = Environment.TickCount; if ((tickCount % 2) == 0) { test.AddLog("Querying using MobileServiceTableQuery<T>.ToEnumerableAsync"); readMovies = await query.ToEnumerableAsync(); } else { test.AddLog("Querying using IMobileServiceTable<T>.ReadAsync(MobileServiceTableQuery<U>)"); readMovies = await table.ReadAsync(query); } } else { readProjectedMovies = await selectedQuery.ToEnumerableAsync(); } long actualTotalCount = -1; ITotalCountProvider totalCountProvider = (readMovies as ITotalCountProvider) ?? (readProjectedMovies as ITotalCountProvider); if (totalCountProvider != null) { actualTotalCount = totalCountProvider.TotalCount; } IEnumerable <Movie> expectedData = ZumoQueryTestData.AllMovies; if (whereClause != null) { expectedData = expectedData.Where(whereClause.Compile()); } long expectedTotalCount = -1; if (includeTotalCount.HasValue && includeTotalCount.Value) { expectedTotalCount = expectedData.Count(); } if (orderBy != null) { expectedData = ApplyOrdering(expectedData, orderBy); } if (skip.HasValue) { expectedData = expectedData.Skip(skip.Value); } if (top.HasValue) { expectedData = expectedData.Take(top.Value); } if (includeTotalCount.HasValue) { if (expectedTotalCount != actualTotalCount) { test.AddLog("Total count was requested, but the returned value is incorrect: expected={0}, actual={1}", expectedTotalCount, actualTotalCount); return false; } } List <string> errors = new List <string>(); bool expectedDataIsSameAsReadData; if (selectExpression != null) { string[] expectedProjectedData = expectedData.Select(selectExpression.Compile()).ToArray(); expectedDataIsSameAsReadData = Util.CompareArrays(expectedProjectedData, readProjectedMovies.ToArray(), errors); } else { expectedDataIsSameAsReadData = Util.CompareArrays(expectedData.ToArray(), readMovies.ToArray(), errors); } if (!expectedDataIsSameAsReadData) { foreach (var error in errors) { test.AddLog(error); } test.AddLog("Expected data is different"); return false; } else { if (typeof(TExpectedException) == typeof(ExceptionTypeWhichWillNeverBeThrown)) { return true; } else { test.AddLog("Error, test should have failed with {0}, but succeeded.", typeof(TExpectedException).FullName); return false; } } } catch (TExpectedException ex) { test.AddLog("Caught expected exception - {0}: {1}", ex.GetType().FullName, ex.Message); return true; } })); }
private async Task Query <T, U>(Func <IMobileServiceTable <T>, MobileServiceTableQuery <U> > getQuery) { IMobileServiceTable <T> table = GetClient().GetTable <T>(); MobileServiceTableQuery <U> query = getQuery(table); await table.ReadAsync(query); }