none
How to reset a query RRS feed

  • Question

  • Hi all,

    I have an entity.  I know I can add additional filters by using syntax similar to:

    result = ctx.myEntity.AsQueriable;

    result = result.where(x => x.name == varName);

    result = result.where(x => x.city == varCity);

    Now, if I do any action that results in a query (say get a count), the query executes.  This is good.  But how do I reset it so I can start all over again with different varName and varCity?

    Yes, I know that I can do both in one statement.  Assume there is lots more code and that the values are only added in some conditions.  And in others, I add many more.  I just didn't want to confuse the issue with that code.

    Thanks,

    me

     


    me (and yes, I DO mark correct answers)
    Monday, September 19, 2011 8:32 PM

All replies

  • Hello,

    Thank you for posting.

    From your description, it looks you would like to iterate the collection and print each element onto others. If yes, I think we just add a foreach to loop this collection and output each element.

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, September 21, 2011 6:34 AM
  • Larcolais,

    Thanks for the reply.  I think I didn't make the issue clear enough.

    Lets say I create an instance of:

    myQuery = this.roCtx.mastername_index.AsQueryable();
    

    Now I do the following:

    myQuery = myQuery.Where(mni => mni.Name.Last == "Smith");
    myQuery = myQuery.Where(mni => mni.Name.First == "John");
    Console.WriteLine("We have {0} records.", myQuery.Count());
    myQuery = myQuery.Where(mni => mni.Name.Last == "Jones");
    myQuery = myQuery.Where(mni => mni.Name.First == "Sue");
    Console.WriteLine("We have {0} records.", myQuery.Count());

    The 1st Console.WriteLine gives me what I expect.  The second gives me 0 records because the where clause now contains 4 conditions (ANDed together and the Last name can't = 2 different values at the same time). 

    What do I need to write after the 1st Console.WriteLine statement so that the cache is reset and the SQL for the next Console.WriteLine only has 2 conditions (searching only for Sue Jones)?

    I guess I could release and reinstantiate myQuery, but I am hoping there is a simple clearCache type command I can use instead.

    Also, as an aside, when constructing a where clause as above, is there a way to specify that the conditions be ORed instead of ANDed together? 

    Thanks,

    me

     

     

     


    me (and yes, I DO mark correct answers)
    Wednesday, September 21, 2011 6:41 PM
  • Hello again,

    Thanks for your detailed reply.

    You can directly use "&&" or "||" operator to compose and filter the conditions for WHERE extension method like this.

                var query = (from p in datactx.Childrens
                             select p).ToList();
                var newQuery = query.Where(x => x.age >= 20 && x.id <= 10).ToList();
                this.dataGridView1.DataSource = newQuery;
    

    Hope this helps.

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 22, 2011 5:49 AM
  • Larcolais,

    I am aware that I can combine the where clauses.  But in my situation, the where clause is built on user input.  As the user provides details, I build the where string.

    So the user fills in a last name field.  I add a search on last name.  Then they fill in a first name field, so I add that.  Finally, they say search.  So I have to build the search "on the fly".

    When they are done with that record, they start a new one.  So I have to reset the search criteria so the previous criteria doesn't get in the way.

    The code simulates that process without extraneous code in the way....

    Thanks,

    me


    me (and yes, I DO mark correct answers)
    Thursday, September 22, 2011 4:26 PM
  • Hello,

    I think you can check DLinq tech to achieve your goal. Please check this blog for more information. http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

    Please feel free to let me know how it goes.

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Larcolais Gong Monday, September 26, 2011 2:09 AM
    • Unmarked as answer by FletcherJ Wednesday, September 28, 2011 10:01 PM
    Friday, September 23, 2011 7:12 AM
  • Larcolais,

    That link may have my answer, but if so, I can't find it.  I looked at some of his other blogs and they too were quite good, except that they still didn't show me how to address this issue.

    I am sure there is a simple method call the clears the cache that I could make on the line following the first Console.WriteLine.  I just need to know what it is.

    The alternative is to destroy and reinstantiate the myQuery object - but this seems to result in a lot of code having to run when all I want to do is change the "where" criteria.

    Thanks,

    me


    me (and yes, I DO mark correct answers)
    Wednesday, September 28, 2011 7:21 PM