none
Casting Linq to Dataset <anonymous type> results to a data type for processing RRS feed

  • Question

  • While debugging the following:


    Dim Query = From p In dtClientsPrior Group p By clientId = p.Field(Of Int32)("Client_ID") Into Count() Where Count > 1
    For Each row As Object In Query
     Debug.Print(row.GetType.ToString)
    Next

    When execution is stopped on the debug print line and hovering the mouse over the row varible the following displays:

    row clientId=57001224, count=2
      <anonymous type> clientId=57001224, count=2

    My question is how-to cast the <anonymous type> clientId and count to a data type in the loop to process each clientId in the output? 


    Or how-to add linq statements to the Dim query statement to get non-anonymous type results?
    • Edited by Yogi Thursday, December 6, 2012 5:28 PM additional info added
    Thursday, December 6, 2012 5:21 PM

Answers

  • Hi Yogi;

    Pieter is correct you will need to implement a type you want to be returned from the query. I have modified your query to show how the query can return a Type IEnumerable(Of QueryResults).

    Dim Query = From p In dtClientsPrior
                Group p By clientId = p.Field(Of Int32)("Client_ID") Into Count() 
                Where Count > 1
                Select New QueryResults With
                	{
                			.ClientID = clientId,
                			.Count = Count
                	}
                
    Public Class QueryResults
        Public ClientID As Integer
        Public Count As Integer
    End Class  

      


    Fernando (MCSD)

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

    • Marked as answer by Yogi Friday, December 7, 2012 10:36 AM
    Thursday, December 6, 2012 7:59 PM

All replies

  • Define a named class that implements the correct public fields, and then perform the cast to it.

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    We are here to learn, to share knowledge, and to earn points; all in about equal measure.

    Thursday, December 6, 2012 6:41 PM
  • Hi Yogi;

    Pieter is correct you will need to implement a type you want to be returned from the query. I have modified your query to show how the query can return a Type IEnumerable(Of QueryResults).

    Dim Query = From p In dtClientsPrior
                Group p By clientId = p.Field(Of Int32)("Client_ID") Into Count() 
                Where Count > 1
                Select New QueryResults With
                	{
                			.ClientID = clientId,
                			.Count = Count
                	}
                
    Public Class QueryResults
        Public ClientID As Integer
        Public Count As Integer
    End Class  

      


    Fernando (MCSD)

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

    • Marked as answer by Yogi Friday, December 7, 2012 10:36 AM
    Thursday, December 6, 2012 7:59 PM
  • I Agree with previous 2 replies, you have to Enumerate the result that came out from the datatable, I do that by :

    Dim Query = From p In dtClientsPrior.AsEnumerable Group p By clientId = p.Field(Of Int32)("Client_ID") Into Count() Where Count > 1 Select p


    Blog: http://samir-ibrahim.blogspot.com/


    Friday, December 7, 2012 10:22 AM