return value from LINQ query

Last post 07-04-2009 8:22 PM by bhadelia.imran. 3 replies.

Sort Posts:

  • return value from LINQ query

    07-04-2009, 5:22 AM
    • Member
      570 point Member
    • muybn
    • Member since 08-06-2005, 7:28 AM
    • Salt Lake City, UT
    • Posts 863

    I want to set a label's text equal to a value I return from a LINQ query, as follows:

            Dim dc As New alumniDataContext
            Dim data As tblReunion_Info
            lbl = Util.FindChild(Page, "lblEntry")
            data = (From r In dc.tblReunion_Infos _
             Where r.ReunionIndex = Request.QueryString("index") _
             Select r.ReunionYear, r.ReunionIndex)
            lbl.Text = data.ReunionYear
    

    Following is the error I receive, which obviously means that I have to do some converting somewhere. Your help will be appreciated.

    Unable to cast object of type 'System.Data.Linq.DataQuery`[System.String,System.Int32]' to type 'tblReunion_Info'."

  • Re: return value from LINQ query

    07-04-2009, 1:18 PM
    Answer
    • Contributor
      2,170 point Contributor
    • JMayo
    • Member since 02-23-2007, 4:13 AM
    • Denver, CO
    • Posts 282
    • TrustedFriends-MVPs

    Hi,

    Try this:

    04.data = (From r In dc.tblReunion_Infos _   
    05. Where r.ReunionIndex = Request.QueryString("index") _   
    06. Select r.ReunionYear, r.ReunionIndex).FirstOrDefault

    Your query returns an IQueryable(Of Customer), which is a collection and you need to either iterate through it with a for/for each loop or use a query operator like FirstOrDefault to extract the one item you need.

    Joe

  • Re: return value from LINQ query

    07-04-2009, 3:46 PM
    • Member
      570 point Member
    • muybn
    • Member since 08-06-2005, 7:28 AM
    • Salt Lake City, UT
    • Posts 863

    Thanks for your efforts, but it didn't like the "FirstOrDefault" after the final parenthesis; the closest I could get was the following, which still returned the same error as listed in my first entry.

    Dim strQS As String = Request.QueryString("index")
    lbl = Util.FindChild(Page, "lblEntry")
    Dim dc As alumniDataContext = New alumniDataContext
    Dim data As New tblReunion_Info '= New tblReunion_Info
    data = (From r In dc.tblReunion_Infos _
       Where r.ReunionIndex = strQS _
       Select r.ReunionYear.FirstOrDefault, r.ReunionIndex)
    lbl.Text = data.ReunionYear
    


     

  • Re: return value from LINQ query

    07-04-2009, 8:22 PM
    Answer
    • Contributor
      2,861 point Contributor
    • bhadelia.imran
    • Member since 08-04-2008, 2:59 AM
    • India
    • Posts 477
    1. (From r In dc.tblReunion_Infos _  
    2.    Where r.ReunionIndex = strQS _  
    3.    Select r.ReunionYear, r.ReunionIndex).FirstOrDefault()

    There must be FirstOrDefault function, please check it again.

    Without making call to any ToList, First, FirstOrDefault, query cant get execute, First of FirstOrDefault will returns the first element after executing your query.

    Imran
    [MCTS]
    Born to fly High
    http://knowledgebaseworld.blogspot.com/
Page 1 of 1 (4 items)