0

I am trying to use Dynamic Linq library in my code, but it gives this error

'UserId' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly. Near simple identifier, line 6, column 1

and here is my code

 TestDB db = new TestDB();
        string filter = "UserId == 15";
        //var searchResult =
        GridView1.DataSource = from x in db.SearchSummaries.Where(filter)
                               select x;
        GridView1.DataBind();
cuongle
  • 74,024
  • 28
  • 151
  • 206
Waqas
  • 424
  • 7
  • 15

2 Answers2

2

Not so familiar with dynamic Linq but from your error message:

'UserId' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly. Near simple identifier, line 6, column 1

Please try this:

1.) Is the column UserId a Integer and not a String? Mabye you need to use:

    string filter = "UserId='15'";

2.) Try to pass in the filter parameter as a second argument:

    GridView1.DataSource = db.SearchSummaries.Where("UserId = @0", 15); 

3.) I don't know if you are able to run "regular" Linq queries, but if you are, try:

    GridView1.DataSource = db.SearchSummaries.Where(search => search.UserId == 15);                               
    GridView1.DataBind();
nekman
  • 1,919
  • 2
  • 15
  • 26
  • solution # 2 worked but I want to create a complete where clause based on filters e.g username like '%something%' and firstname like '%some%'etc. How to go about it? – Waqas Sep 27 '12 at 18:32
  • I think you should be able to use something like: db.SearchSummaries.Where("UserId.Contains(@0) AND UserName.Contains(@1)", 15, "Foo"); You can read more about Dynamic Linq, and see examples here: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx – nekman Sep 27 '12 at 18:41
  • Nice, I hope you find what you look for. Good luck! :) – nekman Sep 27 '12 at 19:52
1

Try this:

TestDB db = new TestDB();
    string filter = "xi => xi.UserId == 15";
    //var searchResult =
    GridView1.DataSource = from x in db.SearchSummaries.Where(filter)
                           select x;
    GridView1.DataBind();

Or this:

TestDB db = new TestDB();
    string filter = "UserId=15";
    //var searchResult =
    GridView1.DataSource = from x in db.SearchSummaries.Where(filter)
                           select x;
    GridView1.DataBind();

EDIT: I realize this isn't dynamic linq...but it should work regardless as long as your data structure is correct. Could you post that?

Mansfield
  • 14,445
  • 18
  • 76
  • 112
  • that didn't work, same error. It works if i use lambda expressions or pass it as parameter – Waqas Sep 27 '12 at 18:35
  • Can you post your data structure? It's difficult to tell more without seeing it...see nekman's answer. – Mansfield Sep 27 '12 at 18:50