using IN(or similar) condition in an azure storage query

474 views Asked by At

I have an object of type Person which has only two properties: Name and Place. I have an azure table of the same name with the same two columns and an additional column called Date. I have to fetch date based on condition that either the name or place exist in table.I have seperate lists for name and place from wherein it should match the value. For example:

//some data into these lists
    List<string> nameList = new List<string>(); 
    List<string> placeList = new List<string>();

After some research, I have tried something like this:

IEnumerable<Person> query = (from p in table.CreateQuery<Person>() where nameList .Contains(p.Name)|| placeList .Contains(p.Place)  select p);

But this gives an error saying not supported. What would be the best way to achieve this? Please note the name and place list can contain 1000s of entries.

Update

This is the error message :

The expression (value(System.Collections.Generic.List`1[System.String]).Contains([10007].Name) Or
value(System.Collections.Generic.List`1[System.String]).Contains([10007].Place)) is not supported.
1

There are 1 answers

0
nitinvertigo On BEST ANSWER

I have finally figured it out. I used the GenerateFilterCondition and the CombineFilters to achieve it.

Code:

    string queryString = String.Empty;
    string query1 = null;
    for(int i=0;i<nameList.Count;i++)
     {
      queryString  = TableQuery.GenerateFilterCondition("Name",QueryComparisons.Equal,nameList[i].ToString());
     if (i == 0)
      query1 = queryString;
      else
      query1 = TableQuery.CombineFilters(query1, TableOperators.Or, queryString);
      }
   queryString = string.Empty;
   for (int j = 0; j < placeList.Count;j++ )
     {
      queryString = TableQuery.GenerateFilterCondition("Place", QueryComparisons.Equal, placeList[j].ToString());
    query1 = TableQuery.CombineFilters(query1, TableOperators.Or, queryString);
    }
    TableQuery<Person> tblQuery = new TableQuery<Person>().Where(query1);
    List<Person> evntHst = table.ExecuteQuery(tblQuery).OrderByDescending(t => t.Timestamp).ToList();

Hope it helps others! Cheers!!