none
How to pass LINQ Query with Joins and Anonomous Types to a procedure? RRS feed

  • Question

  • If you have the results of a LINQ query …

    Dim SampleQuery = From SQ in DC.Authors select SQ

     you can pass it to a procedure as shown below:

    DoSomething(SampleQuery)

    …..

    private sub DoSomething(TDS as IQueryable(of Author))

    How would I pass the following LINQ query:

    Dim StoryQuery = From SD In DC.Stories Join AD In DC.Authors On SD.AuthorID Equals AD.AuthorID Join BD In DC.Books On SD.BookID Equals BD.BookID Order By SD.Title Select New With {.BookName = BD.Name, .AuthorName = AD.Name, .StoryTitle = SD.Title}

     

     

     

    Friday, June 24, 2011 9:33 PM

Answers

  • Hi AirForce9797;

    Basically just create a new class that will be created by the query and the results of the query will be of IQueryable(of ThatClass). For example here is a code snippet from your question.

    Dim StoryQuery = From SD In DC.Stories 
             Join AD In DC.Authors On SD.AuthorID Equals AD.AuthorID 
             Join BD In DC.Books On SD.BookID Equals BD.BookID 
             Order By SD.Title 
             Select New StoryItem With 
             {
               .BookName = BD.Name, 
               .AuthorName = AD.Name, 
               .StoryTitle = SD.Title
             }
             
    Public Class StoryItem
      Public BookName As String
      Public AuthorName As String
      Public StoryTitle As String
    End Class
    

    Please note the addition to the Select clause in the above query. The query will create a new StoryItem for each item it returns. The you can use that as a parameter in a function call as follows:

    private sub DoSomething(TDS as IQueryable(of StoryItem))

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by AirForce9797 Saturday, June 25, 2011 10:22 AM
    Saturday, June 25, 2011 4:03 AM

All replies

  • Hi AirForce9797;

    Basically just create a new class that will be created by the query and the results of the query will be of IQueryable(of ThatClass). For example here is a code snippet from your question.

    Dim StoryQuery = From SD In DC.Stories 
             Join AD In DC.Authors On SD.AuthorID Equals AD.AuthorID 
             Join BD In DC.Books On SD.BookID Equals BD.BookID 
             Order By SD.Title 
             Select New StoryItem With 
             {
               .BookName = BD.Name, 
               .AuthorName = AD.Name, 
               .StoryTitle = SD.Title
             }
             
    Public Class StoryItem
      Public BookName As String
      Public AuthorName As String
      Public StoryTitle As String
    End Class
    

    Please note the addition to the Select clause in the above query. The query will create a new StoryItem for each item it returns. The you can use that as a parameter in a function call as follows:

    private sub DoSomething(TDS as IQueryable(of StoryItem))

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by AirForce9797 Saturday, June 25, 2011 10:22 AM
    Saturday, June 25, 2011 4:03 AM
  • Thanks that worked. I had forgotten to place the name of the class between the NEW and WITH in the LINQ query.
    Saturday, June 25, 2011 10:23 AM
  • Not a problem, glad I was able to help.

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, June 25, 2011 1:32 PM