none
LINQ to SQL Casting Problem RRS feed

  • Question

  •  

    Hello,

    I am trying to create a 3-tiered web application using Visual Basic 2008 and LINQ instead SQL and am having a problem with the GridView in the presentation layer not able to display the query results because of a casting problem. Here is the problem:

     

    I created a web app placing the Northwind db in the App_Data folder. I then created two folders under the App_Code folder. A BLL folder that will contain the business logic classes and a DAL folder which will contain the Northwind.dbml file that acts as the data access layer. I am following the architecture logic of the data tutorials written by Scott Mitchell that are posted on the ASP.NET website data access tutorials section. I created the Data Access Layer (DAL) by using the Object Relational Designer dragging the Products and Categories tables onto the designer surface. I then added a class file under the BLL folder named ProductsBLL to handle all requests for data pertaining to the Products table. I placed an ObjectDataSource and a GridView on the web page to display the data and pointed the ObjectDataSource to the ProductsBLL class and the method listed below:

     

        <System.ComponentModel.DataObjectMethodAttribute(ComponentModel.DataObjectMethodType.Select, False)> _

        Public Function GetProducts() As IEnumerable(Of Product)

            Dim db As New NorthwindDataContext

            Dim qry = From p In db.Products _

                      Select New With {p.ProductName, p.UnitPrice,

                                       p.UnitsInStock}

            Return qry

        End Function

     

    I am attempting to return only 3 fields of data instead of the entire Products table. When I run this web app and attempt to display the ProductName, UnitPrice, and UnitsInStock fields in a GridView I get the following error:

     

    Unable to cast object of type 'System.Data.Linq.DataQuery`1[VB$AnonymousType_0`3[System.String,System.Nullable`1[System.Decimal],System.Nullable`1[System.Int16]]]' to type 'System.Collections.Generic.IEnumerable`1[DAL.Product]'.

     

    I also tried to return qry.AsEnumerable() and I still get the same error. If I do a Select p in the above LINQ query instead of Select New With {p.ProductName, p.UnitPrice,

                                       p.UnitsInStock}

    and ask to return the entire table, the call to the GetProducts() method works fine.

     

     

    Any help would be appreciated on how to fix this problem.

    Friday, September 26, 2008 8:12 PM

Answers

  • You've described the method GetProducts are returning the type IEnuemerable(Of Product) yet you are trying to return the query that is type IEnumerable(Of ???).  You can't even say the name of return type since you select the columns using

    'New With', which is an anonymous type.  Instead you should declare a type with just the fields you want and use an object initiallizer expression.

     

    Sunday, September 28, 2008 2:24 AM
    Moderator

All replies

  • You've described the method GetProducts are returning the type IEnuemerable(Of Product) yet you are trying to return the query that is type IEnumerable(Of ???).  You can't even say the name of return type since you select the columns using

    'New With', which is an anonymous type.  Instead you should declare a type with just the fields you want and use an object initiallizer expression.

     

    Sunday, September 28, 2008 2:24 AM
    Moderator
  • Thanks for your help Matt.

    Sunday, September 28, 2008 6:30 PM