biddulph.r
biddulph.r

Reputation: 5266

Iterating through Collection to build a Where statement for Azure Table Query on Windows Phone 8

I have a query I need to create to get a list of objects (Lets call them Cats) from a database stored remotely through Microsoft Azure on Windows Phone 8.

In order to get this list of objects (Cats) I have to iterate through a list of other objects to pull out the foreign key, lets call it a list of CatHomes. But I also want all cats called Bob.

I know how to get all Cats called Bob:

  private async void getCatsForCatHomesOrCalledBob(Collection<CatHome> homes)
  {
    // get a list of cats that have visited the cat homes in the collection
    IMobileServiceTableQuery<Cat> query = catTable
                                            .Where(cat => cat.Name == "Bob");

    Collection<Cat> items = null;
    // add the items to a list
    items = await query.ToCollectionAsync();
  }

but how do I also iterate through the Collection of CatHome objects and add them to the query, to build it up?

I am aware that I could do something like:

IMobileServiceTableQuery<Cat> query = 
   catTable.Where(cat => cat.Name == "Bob" || cat.HomeId == 1
                    || cat.HomeId == 2);

Which doesn't allow me to iterate through the Collection, what I want to do is something like:

IMobileServiceTableQuery<Cat> query = catTable.Where(cat => cat.Name == "Bob");
foreach(CatHome home in homes){
  query.AddOrStatement(cat.HomeId == home.Id);
}

But cannot find a way to do this.

Is there an alternative? Ideally I would like to be able to pass a String variable to the Where(), so as I can build up the SQL statement as a String, but cannot find a means to do that either.

There is similar functionality for Android and iOS where you can build up the expression using .Or() or .And() methods, which aren't present in IMobileServiceTableQuery in WP8, and things like Skip(), which is present.

I am aware that I could iterate through the list of CatHome objects and do a query for each one, adding the results to a master list, but that seems like a bad use of the phone's resources.

Any help would be appreciated.

Upvotes: 0

Views: 824

Answers (2)

Alexandr Mihalciuc
Alexandr Mihalciuc

Reputation: 2577

Here it is already answered Linq query with Array in where clause?. Get the list of foreign keys first and then use it in contains where clause

Upvotes: 0

Stefan Wexel
Stefan Wexel

Reputation: 1154

Assuming homesin foreach(CatHome home in homes){ is a list you can just do this:

IMobileServiceTableQuery<Cat> query = catTable.Where(cat => cat.Name == "Bob" && homes.Contains(cat.HomeId));

Not sure if the IEnumerable.Contains works to, but converting homes into a List<CatHome> should be easy.

Upvotes: 1

Related Questions