none
Question on Context.ExecuteQuery RRS feed

  • Question

  • In Linq  I am executing a custom query using ExecuteQuery() method, which requires a "Of class" to typecast returned records from the query. I need to pre-define the class in code as seen below.

     

    Q: Can i live without defining the BlogRecords class using some annonmynous types technique or someway where it could create class on it own based on column names returned by the query.

     

    Code Snippet

    Dim x = dal.ExecuteQuery(Of BlogRecords)("SELECT YEAR(BlogDate) AS YearNo,MONTH(BlogDate) AS MonthNo, From Blogs")

    GridView1.DataSource = x

    GridView1.DataBind()

     

     

    Public Class BlogRecords

    Private _YearNo As Integer

    Public Property YearNo() As Integer

    Get

    Return _YearNo

    End Get

    Set(ByVal value As Integer)

    _YearNo = value

    End Set

    End Property

     

    Private _MonthNo As Integer

    Public Property MonthNo() As Integer

    Get

    Return _MonthNo

    End Get

    Set(ByVal value As Integer)

    _MonthNo = value

    End Set

    End Property

    End Class

     

     

     

    Thursday, July 10, 2008 11:35 AM

Answers

  • LINQ to SQL is designed around a model where you declare types with the anonymous aspect being useful when you need to return unusual shapes from LINQ queries. Because the query is parsable at compile time it can work out, again at compile time, a new anonymous type for you.

     

    As you are not using LINQ queries but SQL syntax there is no way the compiler can build you an anonymous type.

     

    If you wish to use SQL syntax and not create types then ADO.NET is a better choice.

     

    [)amien

    Friday, July 11, 2008 8:01 PM
    Moderator

All replies

  • LINQ to SQL is designed around a model where you declare types with the anonymous aspect being useful when you need to return unusual shapes from LINQ queries. Because the query is parsable at compile time it can work out, again at compile time, a new anonymous type for you.

     

    As you are not using LINQ queries but SQL syntax there is no way the compiler can build you an anonymous type.

     

    If you wish to use SQL syntax and not create types then ADO.NET is a better choice.

     

    [)amien

    Friday, July 11, 2008 8:01 PM
    Moderator
  • Interesting ...

     

    But my feeling is not all things are possible with Linq queries eg.  A simple thing i wanted to achieve was

    Bring output of query as "Jan-2008", "Feb-2008" etc.

     

    I tried a syntax something like this in linq query : from p in ....... ... Select  p.BlogDate.Value.toString("MMM-yyyy") , but linq return back with error that it can;t find corrosponding sql query method to get date into MMM-yyyy format... (Don't remember the exact error right now)

     

    Anyone has a comment on this ?

     

    Regards

    Jignesh.

    Wednesday, July 16, 2008 3:18 PM
  • That is correct - LINQ to SQL supports only specific methods of the String class (for example) - it doesn't support all/arbitrary methods.

     

    You can process the data locally after retrieving it from the database, and that will allow you to apply the kind of formatting you desire.

     

    Thanks,

    --Samir

     

     

    Wednesday, July 16, 2008 5:54 PM