954,551 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Return an anonymous type in my Method

Hi i'm creating my Data Access Layer and I want to return my object but not all the fields so this is the code i'm using

Public Function BasicSearchProposals(ByVal searchvalue As String) As IEnumerable(Of DCProposal)
            Dim dc As New PPDataContext()

            Dim proposals = From p In dc.DCProposals _
                            Where p.RefNum = searchvalue Or _
                            p.FormattedrefNum = searchvalue Or _
                            p.UniquePropertyReference = searchvalue Or _
                            p.ApplicantName = searchvalue Or _
                            p.AgentName = searchvalue Or _
                            p.ApplicantPostCode = searchvalue Or _
                            p.SitePostCode = searchvalue Or _
                            p.ahDescription.Contains(searchvalue) Or _
                            p.AgentPostCode.Equals(searchvalue) _
                            Select New With { _
                            .RefNum = p.RefNum, _
                            .ApplicantName = p.ApplicantName, _
                            .UniquePropRef = p.UniquePropertyReference}

            Return proposals
        End Function


I then call this method in the Business Logic Layer

Public Shared Function BasicProposalSearch(ByVal searchVal As String) As IEnumerable(Of DCProposal)
            Dim dal As New ProposalDAL()
            Return dal.BasicSearchProposals(searchVal)
        End Function


lastly I try to call in in my user interface and bind it to a DataGridView

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        dgvProposals.DataSource = PP_BLL.BLL.ProposalBLL.BasicProposalSearch(txtSearch.Text)
        
    End Sub


but I keep getting this error
Unable to cast object of type 'System.Data.Linq.DataQuery`1[VB$AnonymousType_0`3[System.String,System.String,System.String]]' to type 'System.Collections.Generic.IEnumerable`1[PP_DAL.DCProposal]'.

please help me to correct it

k1robert
Newbie Poster
20 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

Hi i'm creating my Data Access Layer and I want to return my object but not all the fields so this is the code i'm using

Public Function BasicSearchProposals(ByVal searchvalue As String) As IEnumerable(Of DCProposal)
            Dim dc As New PPDataContext()

            Dim proposals = From p In dc.DCProposals _
                            Where p.RefNum = searchvalue Or _
                            p.FormattedrefNum = searchvalue Or _
                            p.UniquePropertyReference = searchvalue Or _
                            p.ApplicantName = searchvalue Or _
                            p.AgentName = searchvalue Or _
                            p.ApplicantPostCode = searchvalue Or _
                            p.SitePostCode = searchvalue Or _
                            p.ahDescription.Contains(searchvalue) Or _
                            p.AgentPostCode.Equals(searchvalue) _
                            Select New With { _
                            .RefNum = p.RefNum, _
                            .ApplicantName = p.ApplicantName, _
                            .UniquePropRef = p.UniquePropertyReference}

            Return proposals
        End Function

I then call this method in the Business Logic Layer

Public Shared Function BasicProposalSearch(ByVal searchVal As String) As IEnumerable(Of DCProposal)
            Dim dal As New ProposalDAL()
            Return dal.BasicSearchProposals(searchVal)
        End Function

lastly I try to call in in my user interface and bind it to a DataGridView

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        dgvProposals.DataSource = PP_BLL.BLL.ProposalBLL.BasicProposalSearch(txtSearch.Text)
        
    End Sub

but I keep getting this error Unable to cast object of type 'System.Data.Linq.DataQuery`1[VB$AnonymousType_0`3[System.String,System.String,System.String]]' to type 'System.Collections.Generic.IEnumerable`1[PP_DAL.DCProposal]'.

please help me to correct it

Ok figured it out

in the DAL

Public Function BasicSearchProposals(ByVal searchvalue As String) As IQueryable
            Dim dc As New PPDataContext()
            Dim proposals = From p In dc.DCProposals _
                            Where p.RefNum = searchvalue Or _
                            p.FormattedrefNum = searchvalue Or _
                            p.UniquePropertyReference = searchvalue Or _
                            p.ApplicantName = searchvalue Or _
                            p.AgentName = searchvalue Or _
                            p.ApplicantPostCode = searchvalue Or _
                            p.SitePostCode = searchvalue Or _
                            p.ahDescription.Contains(searchvalue) Or _
                            p.AgentPostCode.Equals(searchvalue) _
                            Select New With { _
                            .RefNum = p.RefNum, _
                            .ApplicantName = p.ApplicantName, _
                            .UniquePropRef = p.UniquePropertyReference}

            Return proposals
        End Function


in the BLL

Public Shared Function BasicProposalSearch(ByVal searchVal As String) As IQueryable
            Dim dal As New ProposalDAL()
            Return dal.BasicSearchProposals(searchVal)
        End Function


and lastly in the UI

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        dgvProposals.DataSource = PP_BLL.BLL.ProposalBLL.BasicProposalSearch(txtSearch.Text)
        
    End Sub
k1robert
Newbie Poster
20 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

No problem. Please mark this thread as solved.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: