none
How to search when Iqueryable type is unknown? RRS feed

  • Question

  • Hello,

    I am trying to write a generic function that will allow me to search a table and return the rows where the named field contains the search term.  When I run this code, it tells me that 'where' is not a method of Queryable.  I've been trying to figure it out for about a week now with no luck. 

    For example, at run time, I am asking the user to select a tablename, select the field to search, and type the keyword to search for, I want to be able to call this function and have it return the results.

    I hope i've made my question clear - I'm still quite new to this (using VB also) and am trying to understand everything line by line.  Thanks in advance!

    The code I have been messing with is listed below:

    <System.Runtime.CompilerServices.Extension()> _

    Public Function iLike(ByVal source As IQueryable, ByVal tableName As String, ByVal fieldName As String, ByVal keyword As String) As IQueryable
    Dim Type As Type = Type.GetType("MyNamespace." + tableName)
    Dim Parameter = Expression.Parameter(Type, "Entity")
    Dim [property] = Type.GetProperty(fieldName)
    Dim propertyAccess = Expression.MakeMemberAccess(Parameter, [property])
    Dim findExp = Expression.Lambda(propertyAccess, Parameter)
    Dim methodName As String = "Where"
    Dim resultExp As MethodCallExpression = Expression.[Call](GetType(Queryable), methodName, New Type() {Type, [property].PropertyType}, source.Expression, Expression.Quote(findExp))
    Return (source.Provider.CreateQuery(resultExp))
    End Function

    Tuesday, January 19, 2010 7:09 PM

Answers

All replies

  • You might want to check out the Dynamic LINQ samples included in the Visual Studio samples folder installed on your machine. 

    It includes a set of routines for dynamically creating IQueryable's based on strings.

    [)amien
    Wednesday, January 20, 2010 7:39 AM
    Moderator
  • You might want to check out the Dynamic LINQ samples included in the Visual Studio samples folder installed on your machine. 

    It includes a set of routines for dynamically creating IQueryable's based on strings.

    [)amien

    I did try that, but as soon as I import the code I get about 112 errors in my project.  Not exactly sure why that happens - I've never been able to get the dynamic linq sample to work w/ my actual code.
    Wednesday, January 20, 2010 3:28 PM
  • Hello,

    Could you please provide us with more detailed information about the error you have received?  


    Best Regards,
    Lingzhi Sun
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, January 25, 2010 2:45 PM
    Moderator
  • Hello,

    Could you please provide us with more detailed information about the error you have received?  


    Best Regards,
    Lingzhi Sun
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    My code complies fine without dynamic.vb, but as soon as I import it into any part of my application (Using MVC Framework, not sure if this is an issue) I receive over 100 errors, some of them are as follows:

    Error 1 Type 'System.Web.Mvc.Controller' is not defined. C:\evg\evg\Controllers\SetupController.vb 7 14 evg
    Error 2 'ModelState' is a type and cannot be used as an expression. C:\evg\evg\Controllers\SetupController.vb 12 59 evg
    Error 3 'View' is a type and cannot be used as an expression. C:\evg\evg\Controllers\SetupController.vb 21 16 evg
    Error 4 Name 'ViewData' is not declared. C:\evg\evg\Controllers\SetupController.vb 28 9 evg
    Error 6 'View' is a type and cannot be used as an expression. C:\evg\evg\Controllers\SetupController.vb 30 16 evg

    ...etc, if I remove dynamic.vb, the errors go away and everything works as expected.
    Monday, January 25, 2010 2:56 PM
  • Hello,

    Please have a check on my reply in this related thread, http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/11dbd8aa-abcd-44bd-bbb2-eea60edf77d9.  Does it solve the problem? 


    Thanks,
    Lingzhi Sun
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by allmixedup Thursday, January 28, 2010 5:04 PM
    Wednesday, January 27, 2010 2:21 PM
    Moderator
  • Hello,

    Please have a check on my reply in this related thread, http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/11dbd8aa-abcd-44bd-bbb2-eea60edf77d9.  Does it solve the problem? 


    Thanks,
    Lingzhi Sun
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    thank you sir!
    Thursday, January 28, 2010 5:04 PM
  • Hello,

    Please have a check on my reply in this related thread, http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/11dbd8aa-abcd-44bd-bbb2-eea60edf77d9.  Does it solve the problem


    Thanks,
    Lingzhi Sun
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    Is there something wrong with the link? It cannot be opened, Is the link invalid?
    Sunday, August 8, 2010 2:20 AM