none
Simple select with where RRS feed

  • Question

  • I am struggling with this for what reason I don't know. I am trying to return an object using ObjectQuery like so

     Dim query As ObjectQuery(Of fullname) = context.fullnames.Where("it.symbol= symbol", New ObjectParameter("symbol", tbSymbol.Text))
    It doesn't like the tbSymbol.text even though its a valid text box. If I hard code it to a symbol value, i still cannot use query.idfullname or query.fullcompanyname. Why? Isn't query now an object(Of fullname)?


    Thomas Donino Visual Studio 2010 on Windows 7
    Wednesday, October 5, 2011 11:12 AM

Answers

  • Also, what type of object is "context"?  Is it an ObjectContext or a dbContext (EF 4.1)?  It may be that the new dbSet<T> objectsets don't support those features in the Where method.  So maybe like Patrice said, just use the method you found out that works.
    Tom Overton
    • Marked as answer by dinotom Thursday, October 6, 2011 12:21 AM
    Wednesday, October 5, 2011 6:17 PM

All replies

  • Hi dinotom,

    Try your query like this:

    Dim oValue As New Object()
    oValue = tbSymbol.Text
    
    Dim query As ObjectQuery(Of fullname) = context.fullnames.Where("it.symbol= @symbol", New ObjectParameter("symbol", oValue))

     


    Tom Overton
    Wednesday, October 5, 2011 2:06 PM
  • Hi,

    In addition to Tom the reason is that the query is ultimately translated to a SQL statement. But this query references a TextBox control making impossible to translate this to a SQL statement.

    By using an intermediate variable, you remove the reference to the TextBox control from the query allowing it to be translated (or if it sill doesn"t work using the actual type of the parameter ie a string , an integer or whatever).

    Also it's best to always tell the exact error message you have. It allows to quickly check the assumption (for example some code could have multiple errors and one could see one error when you have actually another one less obvious error showing up first).

     

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    Wednesday, October 5, 2011 3:20 PM
  • Thank you. It doesn't work as I get an Overload resolution failed because no accesible "Where" accepts this number of arguments error

    However, I got this to work

    targFullName = From fn In context.fullnames Select fn Where fn.symbol = tbSymbol.Text.ToUpper()



    Thomas Donino Visual Studio 2010 on Windows 7
    Wednesday, October 5, 2011 4:29 PM
  • see my answer to Tom, Id like to know why Im getting that error message
    Thomas Donino Visual Studio 2010 on Windows 7
    Wednesday, October 5, 2011 4:32 PM
  • That's odd you're getting that error.  It worked for me, using that same format.  I found another thread with somebody getting that same error (a while back with vs2008), maybe it has something to do with you needing some updated files for using dynamic linq?

    http://social.msdn.microsoft.com/Forums/en-NZ/linqtosql/thread/18fc67c9-f1d6-43e3-90ca-62f945984f0d


    Tom Overton
    Wednesday, October 5, 2011 4:47 PM
  • What if you click "Go to Definition" on the Where method ? I'm seeing a :

    Public Function Where(predicate As

    String, ParamArray parameters() As System.Data.Objects.ObjectParameter)

    signature and the code works fine... Else do the same thing on ObjectParameter to make sure this is not another class named ObjectParameter...

    Anway unless you really need something dynamic your current option seems better to me.

    Sorry for my first response. I saw sometimes problems when using too much specific code in the query but I guess that this is not a problem as long as it returns a primitive type and I really thought Tom was right. As you see knowing the error allows to clarify the issue...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Wednesday, October 5, 2011 5:35 PM
  • Also, what type of object is "context"?  Is it an ObjectContext or a dbContext (EF 4.1)?  It may be that the new dbSet<T> objectsets don't support those features in the Where method.  So maybe like Patrice said, just use the method you found out that works.
    Tom Overton
    • Marked as answer by dinotom Thursday, October 6, 2011 12:21 AM
    Wednesday, October 5, 2011 6:17 PM