none
Additional columns returned RRS feed

  • Question

  • My database has four columns (CustomerID, CustomerFirstname, CustomerLastname, Age). I then create the below query:

        Public Function GetCustomers() As System.Collections.Generic.IEnumerable(Of CustomerDTOImplements ICustomer.GetCustomers
            Dim ctx As New DbDataContext
     
            Dim query = From c In ctx.Customers
                        Select New CustomerDTO With {
                            .CustomerID = c.CustomerID,
                            .CustomerFirstname = c.CustomerFirstName}
     
            Return query
        End Function

    When i attach the above query to a gridview, i was expecting it to return two columns. Instead it returns all the columns that i have not specified (lastname, age). Although i understand the schema on the Linq file has all the columns listed, how would i have retrieved only the required columns?

    Thanks

    Wednesday, December 19, 2012 10:50 AM

Answers

  • Hi Pure Deal;

    When you tell the query to return an instance of CustomerDTO it will create an object of CustomerDTO for each object returned and therefore all four fields are part of that object.  

    To your question, " how would i have retrieved only the required columns?", create a class that just has the two columns and use that class in the query in place of CustomerDTO. The only other way is to remove the CustomerDTO class name from the query altogether which will then return an anonymous type but seeming you are returning this result to a caller of this function the caller would need to use Reflections class to find out what is in the returned object.

    Also please return to your post, "Unable to cast object of type 'System.Data.Linq.DataQuery`1[LinqProject.CustomerDTO]' to type 'LinqProject.CustomerDTO'", and respond to my post.

       


    Fernando (MCSD)

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

    • Proposed as answer by Shyam Kr Wednesday, December 19, 2012 2:31 PM
    • Marked as answer by Pure Deal Wednesday, December 19, 2012 3:58 PM
    Wednesday, December 19, 2012 2:18 PM
  • By returning a list of specific type and use GridView default behavior (Or column auto generation) you force it to display all Properties of your object.
    If you don't need all columns, you can return your list not as CustomerDTO but as anonymous type casted to object.

    I'm not familiar with VB so I used C# to VB conversation tool and hope my example syntax is correct.

    Dim query = From c In CustomersDirectCast(New With { _
    	Key .CustomerID = c.CustomerID, _
    	Key .CustomerFirstname = c.CustomerFirstName _
    }, Object)


    Please Mark as Reply and Vote as Helpful if I helped.

    Also please visit my blog http://msguy.net/

    • Marked as answer by Pure Deal Wednesday, December 19, 2012 3:58 PM
    Wednesday, December 19, 2012 2:35 PM

All replies

  • Hi Pure Deal;

    When you tell the query to return an instance of CustomerDTO it will create an object of CustomerDTO for each object returned and therefore all four fields are part of that object.  

    To your question, " how would i have retrieved only the required columns?", create a class that just has the two columns and use that class in the query in place of CustomerDTO. The only other way is to remove the CustomerDTO class name from the query altogether which will then return an anonymous type but seeming you are returning this result to a caller of this function the caller would need to use Reflections class to find out what is in the returned object.

    Also please return to your post, "Unable to cast object of type 'System.Data.Linq.DataQuery`1[LinqProject.CustomerDTO]' to type 'LinqProject.CustomerDTO'", and respond to my post.

       


    Fernando (MCSD)

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

    • Proposed as answer by Shyam Kr Wednesday, December 19, 2012 2:31 PM
    • Marked as answer by Pure Deal Wednesday, December 19, 2012 3:58 PM
    Wednesday, December 19, 2012 2:18 PM
  • By returning a list of specific type and use GridView default behavior (Or column auto generation) you force it to display all Properties of your object.
    If you don't need all columns, you can return your list not as CustomerDTO but as anonymous type casted to object.

    I'm not familiar with VB so I used C# to VB conversation tool and hope my example syntax is correct.

    Dim query = From c In CustomersDirectCast(New With { _
    	Key .CustomerID = c.CustomerID, _
    	Key .CustomerFirstname = c.CustomerFirstName _
    }, Object)


    Please Mark as Reply and Vote as Helpful if I helped.

    Also please visit my blog http://msguy.net/

    • Marked as answer by Pure Deal Wednesday, December 19, 2012 3:58 PM
    Wednesday, December 19, 2012 2:35 PM
  • Thanks guys for the explanation. When using the above code i get the error "'c' is not declared. It may be inaccessible due to its protection level.".

            Dim query = From c In ctx.Customers
            DirectCast(New With
                       {Key .CustomerID = c.CustomerID, _
                        Key .CustomerFirstname = c.CustomerFirstName}, Object)

    Any idea on that one?

    Thanks

    Wednesday, December 19, 2012 3:58 PM