C# Build where clause dynamically in Linq? for RavenDB -


my code bellow:

this main class :

public class product {     public string id { set; get; }     public ilist<attr> attributes { set; get; } } 

this child class of main class :

public class attr {     public string key { set; get; }     public object value { set; get; } } 

filter item class:

public class filter {     public comparetype type { set; get; }     public string key { set; get; }     public object value { set; get; } } 

linq extension fuction querying :

public static class linqextension {      public static bool ismatch(this product prod, list<filter> filters)     {         foreach(filter f in filters){              attr attribute = prod.attributes.any(a => a.key == f.key);              switch(f.type){                  case comparetype.contain: return ((string)attribute.value).contains(f.value);                  case ....                  default: return false;             }         }     }  } 

filtering products result: (not working)

public actionresult filterproducts(string word, decimal min, decimal max){      list<filter> conditions = new list<filter> {          new filter {key = "price", type = comparetype.between, value = new decimal[] {min, max}  },          new filter {key = "title", type = comparetype.contain, value = word  }          ...          };      var result = session.query<product>().where(p => p.ismatch(conditions)).tolist();     return view(result); } 

when tried run give errors below:

{"could not understand expression: .where(p => p.ismatch(value(app.controllers.homecontroller+<>c__displayclass2).conditions)).tolist()"} 

in general, ravendb's linq provider implementation not equal linq-to-objects provider. under hood, raven's client api serializes linq query experssion lucene query, makes rest call server query. (you can use fiddler see happen)

for example, given database named test northwind sample data , query code (and assuming have fiddler active)

  using (var store = new documentstore         {             url = "http://localhost.fiddler:8080",             defaultdatabase = "test"                     })         {             store.initialize();              using (var session = store.opensession())             {                 var result = session.query<order>().where(x =>                  x.company == "companies/58" && x.freight < 30m).tolist();                                 }         } 

you see following rest call server (after url decoding)

http://localhost:8080/databases/test/indexes/dynamic/orders?&query=company:companies/58 , freight_range:{* dx30}&pagesize=128&sorthint-freight_range=double

what see highlighted in url linq query "serialized" lucene query.

in case error seeing raven's linq implementation cannot understand how transform custom code lucene query


Comments