0

I want to make where class dynamic. When someone will call this function then I don't know the total numbers of parameters and comparison operator(e.g equalto, greaterthen etc) and field-names as well.How can I achieve this?

In this example I am using three parameters. "PartitionKey","RowKey" and tableName.It may be 0 or any numbers and Also it can be any like "FirtName", "Age" etc

public void Persons(string whereClauseParameters)
{            
    var query = (from p in cloudTable.CreateQuery<CustomTableEntity>()
                  where p.PartitionKey == "" && p.RowKey != "" && p.TableName == ""
                  select p);
}
Ali
  • 3,545
  • 11
  • 44
  • 63
  • 2
    possible duplicate of [Dynamic WHERE clause in LINQ](http://stackoverflow.com/questions/848415/dynamic-where-clause-in-linq) – GSerg Aug 27 '15 at 12:53
  • possible duplicate of [Dynamic where clause in LINQ?](http://stackoverflow.com/questions/17789472/dynamic-where-clause-in-linq) – Tim Rogers Aug 27 '15 at 12:53

3 Answers3

0

It sounds you're looking for something like Dynamic Linq. This allows you to do queries like:

var query = northwind.Products
                         .Where("CategoryID = 3 AND UnitPrice > 3")
                         .OrderBy("SupplierID");

where the fields can be specified as dynamic strings, instead of 'hard-coded'.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
0

Something like this?

public void Persons(Predicate<Person> pred)
{
    var query = from p in cloudTable.CreateQuery<CustomTableEntity>()
                where pred(p)
                select p;
}   
dcastro
  • 66,540
  • 21
  • 145
  • 155
0

You can add conditions to a query dynamically. Example:

public void Persons(string firstName, string lastName, int? age) {            

  var query = loudTable.CreateQuery<CustomTableEntity>();

  if (firstName != null) {
    query = query.Where(p => p.FirstName == firstName);
  }

  if (lastName != null) {
    query = query.Where(p => p.LastName == lastName);
  }

  if (age.HasValue) {
    query = query.Where(p => p.Age == age.Value);
  }

  // then use the query

}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005