none
Unable to Cast from Object to Linq query (IEnumerable of <anonymous type?) RRS feed

  • Question

  • Hi I am developing an asp.net web application with VS 2013 preview.

    I need some queries to be in a function in a class then I will use them in many files of my project, but problem is when I try to place a RELATIONAL query in a function then I cannot use even the count property in other files of program, it throws an exception (unable to cast anonymous type2 + + ...)

    this is the function:

        Public Shared Function absents_byStudent(ByVal ID as string)
            Dim log_tableAdapter As New EIDTableAdapters.RCLogTableAdapter
            Dim student_tableAdapter As New EIDTableAdapters.StudentTableAdapter
    
            Dim absents_query = From row In log_tableAdapter.GetDataByStudent(ID)
                                Join srow In student_tableAdapter.GetData On row.StudentID Equals srow.ID
                                Order By srow.ClassCode
                                Order By srow.Lastname 
                                Select srow, row
    
            Return absents_query
        End Function

    VB treat this as an object so I can not do many things with it, in fact I cannot use it anywhere!

    When I point the mouse on absents_query VS says it is Sstem.Collections.Generic.IEnumerable(Of <anonymous type>), so I don't know how to define function data type (because there is no IEnumarable(of <anonymous type>) when I try to do it).

    I also tried to query the result of function:

    Dim q = from row in myclass.absents_byStudent(student_id) Select row

    but it says:

    Error 1 Expression of type 'Object' is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider.

    so I don't know how to cast it as query in code.

    Thanks in Advance

     

     


    • Edited by Javid5555 Friday, September 6, 2013 9:08 AM
    Friday, September 6, 2013 9:02 AM

Answers

  • Hi Javid5555;

    An anonymous type outside of the function in which it was created is difficult to use you must use reflection to get to its members. Therefore the best alternative is to get a strongly typed objects in the results so that you do not have to deal with anonymous types. In your query you can modify the Select clause to return a strongly typed result set as shown below.

    Dim absents_query = From row In log_tableAdapter.GetDataByStudent(ID)
                        Join srow In student_tableAdapter.GetData On row.StudentID Equals srow.ID
                        Order By srow.ClassCode
                        Order By srow.Lastname 
                        Select New StudentData With
                        {
                            .LogTA = srow, 
                            .StudentTA = row
                        }
    
    ' This class helps to make the return type of the query strongly typed
    ' I believe that the types I used below is correct but if not you need to use the table name for each.                   
    Public Class StudentData
        Public LogTA As RCLogTableAdapter
        Public StudentTA As StudentTableAdapter
    End Class     

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Javid5555 Saturday, September 7, 2013 7:22 AM
    Friday, September 6, 2013 2:34 PM

All replies

  • Hi Javid5555;

    An anonymous type outside of the function in which it was created is difficult to use you must use reflection to get to its members. Therefore the best alternative is to get a strongly typed objects in the results so that you do not have to deal with anonymous types. In your query you can modify the Select clause to return a strongly typed result set as shown below.

    Dim absents_query = From row In log_tableAdapter.GetDataByStudent(ID)
                        Join srow In student_tableAdapter.GetData On row.StudentID Equals srow.ID
                        Order By srow.ClassCode
                        Order By srow.Lastname 
                        Select New StudentData With
                        {
                            .LogTA = srow, 
                            .StudentTA = row
                        }
    
    ' This class helps to make the return type of the query strongly typed
    ' I believe that the types I used below is correct but if not you need to use the table name for each.                   
    Public Class StudentData
        Public LogTA As RCLogTableAdapter
        Public StudentTA As StudentTableAdapter
    End Class     

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Javid5555 Saturday, September 7, 2013 7:22 AM
    Friday, September 6, 2013 2:34 PM
  • Hi Javid5555;

    An anonymous type outside of the function in which it was created is difficult to use you must use reflection to get to its members. Therefore the best alternative is to get a strongly typed objects in the results so that you do not have to deal with anonymous types. In your query you can modify the Select clause to return a strongly typed result set as shown below.

    Dim absents_query = From row In log_tableAdapter.GetDataByStudent(ID)
                        Join srow In student_tableAdapter.GetData On row.StudentID Equals srow.ID
                        Order By srow.ClassCode
                        Order By srow.Lastname 
                        Select New StudentData With
                        {
                            .LogTA = srow, 
                            .StudentTA = row
                        }
    
    ' This class helps to make the return type of the query strongly typed
    ' I believe that the types I used below is correct but if not you need to use the table name for each.                   
    Public Class StudentData
        Public LogTA As RCLogTableAdapter
        Public StudentTA As StudentTableAdapter
    End Class     

     

    Thanks you, it helped a lot just a point:

    We should use TableRow instead of TableAdapter and also we should define datatype for function:

    Public Function absents() as

    System.Collections.Generic.IEnumerable(Of Student_Data)

    Saturday, September 7, 2013 7:26 AM