Results 1 to 8 of 8

Thread: Linq join typed to untyped help with group by and count

  1. #1
    dsk96m is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2013
    Posts
    173
    Reputation
    58

    Angry Linq join typed to untyped help with group by and count

    I am trying to write a linq query and having a fit trying to figure it out. Am not very good with linq, just the basics. I code in vb. So I created a datatable in code, then want to write a query that joins it to a typed datatable. Here is what i have so far:
    Code:
            Dim dt As New DataTable
            dt.Columns.Add("cctrid", gettype(integer))
    
           .......Add some rows to dt here.
    
            Dim query = From a In dt
                        Join b In EMS_DS.TEMSCCTR
                        On a.cctrid Equals b.cctr_id
                         Group By a.cctrid Into cctrcnt = Count(a.cctrid)
                         Select a.cctrid, cctrcnt
    
             cctrgc.datasource=query
        End Sub
    I have tried a.field(of integer)("cctrid") and many other things, but I keep getting a type expected error.

    Please help.

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,090
    Reputation
    2804
    I have tried a.field(of integer)("cctrid")
    That would be correct, but Linq needs a 'name', so assign it to a variable, and use that later in query:
    Let a_cctrid = a.Field(Of Integer)("cctrid")
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

  3. #3
    dsk96m is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2013
    Posts
    173
    Reputation
    58
    Where do i put that, in the join or group by?

  4. #4
    dsk96m is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2013
    Posts
    173
    Reputation
    58
    i messed up in the first place

    Code:
            Dim query = From a In dt
                        Join b In EMS_DS.TEMSCCTR
                        On a.cctrid Equals b.cctr_id
                         Group By a.cctrid Into cctrcnt = Count(a.cctrid)
                         Select b.cctr_cd, cctrcnt
    But where do i use what you said?

  5. #5
    dsk96m is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2013
    Posts
    173
    Reputation
    58
    Is it this:
    Code:
            Dim query = From a In dt
                        Let a_cctrid = a.Field(Of Integer)("cctrid")
                        Join b In EMS_DS.TEMSCCTR
                        On a_cctrid Equals b.cctr_id
                        Group a_cctrid By cctr = b.cctr_cd, cctrcnt = a_cctrid Into Group
                        Select New With
                               {
                                   .cctr = cctr,
                                   .cctrcnt = Group.Count()
                               }
    Still get type expected

  6. #6
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,090
    Reputation
    2804
    I don't see the problem.
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

  7. #7
    dsk96m is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2013
    Posts
    173
    Reputation
    58
    Still coming up type expected

  8. #8
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,090
    Reputation
    2804
    Quote Originally Posted by JohnH View Post
    I don't see the problem.
    Here I kind of replicated the code and didn't get any error from compiler. I also didn't see any other problems visually with what you posted. I still don't see the problem or where it could be.
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •