1

I am working with MongoDb, and is using PredicateBuilder class for creating where clause dynamically IN C#. But the exception gets generated as :

[System.ArgumentException] {"Unsupported where clause: ."} ,

and the dynamically generated where clause is

{c => (True AndAlso Invoke(c => (c.ID == value(ASP.search_aspx).txtid.Text), c))} , 

Query Used:

var result = collection.AsQueryable<USER>()
                      .Where(where_clause)
                      .Select(c => new { c.ID, c.COMPANYNAME, c.EMAIL 
                       }).Take(100).ToList();

collection is the instance is MongoCollection.

Code for creating where_clause:

var where_clause = PredicateBuilder.True<GLUSR_USR>();

//object result=0;
if ((txtGlid.Text).Trim() != "")
{
    where_clause = where_clause.And(c => c.GLUSR_USR_ID == txtGlid.Text);
}
if ((txtEmailid.Text).Trim() != "")
{
    where_clause = where_clause.And(c => c.GLUSR_USR_EMAIL == txtEmailid.Text);
}
if ((txtPhone.Text).Trim() != "")
{
    where_clause = where_clause.And(c => c.GLUSR_USR_PH_NUMBER == txtPhone.Text);
}
if ((txtMobile.Text).Trim() != "")
{
    where_clause = where_clause.And(c => c.GLUSR_USR_PH_MOBILE == txtMobile.Text);
}
Jesse
  • 3,522
  • 6
  • 25
  • 40
ank_UD
  • 11
  • 3

2 Answers2

0

Because of the Invoke, it looks like a reference to (ASP.search_aspx).txtid.Text has been captured in the closure, rather than evaluating the value at this time. Can I ask you to evaluating the text box value explicitly in a local, to prevent any chance of the Expression parser making a mess:

if (txtGlid.Text.Trim() != "")
{
    string s = txtGlid.Text;
    where_clause = where_clause.And(c => c.GLUSR_USR_ID == s);
}
it possibly means that you've added `where c.ID == txtid.Text` as a predicate, instead of `where c.ID == txtid.Text()` in your dynamic builder. Passing in the string value should generate the simpler predicate `(True AndAlso c.ID == "someLiteralValue")`, which hopefully can be parsed by the `Linq` provider.
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • but the same is working with the List i.e when collection is of List type – ank_UD Jun 12 '14 at 10:57
  • 1
    And also 'Text' is the property of 'txtid' not the function – ank_UD Jun 12 '14 at 10:59
  • Thanks, have tried your suggestion, but its not working, same error... where_clause = where_clause.And(c => c.GLUSR_USR_ID == "10387904"); – ank_UD Jun 12 '14 at 11:31
  • The mongo query provider might not be able to understand [PredicateBuilder](http://stackoverflow.com/q/7072202/314291) and [here](http://stackoverflow.com/questions/17759613/has-anyone-been-able-to-use-successfully-predicatebuilder-from-albahari-com-agai). You might need to build the dynamic `Expression<>`s yourself :( – StuartLC Jun 12 '14 at 11:39
0

Try to use AsExpandable():

var result = collection.AsQueryable<USER>()
                       .AsExpandable()
                       .Where(where_clause)
                       .Select(c => new { c.ID, c.COMPANYNAME, c.EMAIL 
                       }).Take(100).ToList();

AsExpandable() can be found in LinqKit.

Loetn
  • 3,832
  • 25
  • 41
  • collection is MongoCollection type, it doesn't support AsExpandable(), – ank_UD Jun 12 '14 at 11:14
  • @ank_UD But you are using it on a `IQueryable`, so that can't be the problem. Does it work when you manually set the where clause? – Loetn Jun 12 '14 at 11:18