Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8625627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:50:08+00:00 2026-06-12T07:50:08+00:00

I am using this to get all my records ordered by date: Dim myData

  • 0

I am using this to get all my records ordered by date:

Dim myData = db.Tbl_Exercises.Where(Function(x) x.Exercise_Employee_ID).OrderBy(Function(x) x.Exercise_Create_Date)

I loop through with this:

    For Each i As Tbl_Exercise In myData

        data.Add(New Object() {i.Tbl_Exercise_Type.ExType_Desc, i.Exercise_Duration})

    Next

How can I group by i.Tbl_Exercise_Type.ExType_Desc so I don’t have duplicates?

I tried this:

Dim myData = db.Tbl_Exercises.Where(Function(x) x.Exercise_Employee_ID).GroupBy(Function(x) x.Exercise_Type_ID)

But, I don’t know how to access the variables within my loop.

Thank you.

Edit:

<EmployeeAuthorize()>
Function ExercisePieChartData() As JsonResult

    ' get current employee's id
    Dim db1 As EmployeeDbContext = New EmployeeDbContext
    Dim user1 = db1.Tbl_Employees.Where(Function(e) e.Employee_EmailAddress = User.Identity.Name).Single()
    Dim empId = user1.Employee_ID

    Dim activityGroups = db.Tbl_Exercises.Where(Function(x) x.Exercise_Employee_ID = empId).GroupBy(Function(x) x.Exercise_Type_ID)

    Dim data = New List(Of Object)

    ' columns (headers)
    data.Add(New Object() {"Type", "Duration"})

    ' Iterate through each collection of activities, grouped by activity types
    For Each group In activityGroups

        ' Iterate through each Tbl_Exercise in the group
        For Each activity In group

            ' Do something with an individual element
            data.Add(New Object() {activity.Tbl_Exercise_Type.ExType_Desc, activity.Exercise_Duration})

        Next

    Next

    Return Json(data, JsonRequestBehavior.AllowGet)

End Function

Edit:

    ' Iterate through each collection of activities, grouped by activity types
    For Each group In activityGroups

        Dim type = ""
        Dim duration = 0

        ' Iterate through each Tbl_Exercise in the group
        For Each activity In group

            ' Do something with an individual element
            type = activity.Tbl_Exercise_Type.ExType_Desc
            duration += activity.Exercise_Duration

        Next

        data.Add(New Object() {type, duration})

    Next
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:50:10+00:00Added an answer on June 12, 2026 at 7:50 am

    Note: Your example isn’t entirely clear, so I’m going to make a few assumptions about what you are trying to do with my solution. I’ll modify it as needed as more details are made available.

    From what it looks like, for each employee you are trying to get a collection of types of exercise that they have performed, and with each type of exercise you would also like a collection of the lengths of time that they spent performing these activities.

    I’m going to assume that a row in Tbl_Exercises looks like this:

    Public Class Tbl_Exercise
    
        Public Property Exercise_Employee_ID As Integer
        Public Property Exercise_Type_ID As Integer
        Public Property Exercise_Type_Desc As String
        Public Property Exercise_Duration As Integer
    
    End Class
    

    The attempt you make at the end is pretty close, you just need to actually use the grouping once you’ve created it.

    What the GroupBy method has returned you here is actually an IGrouping(Of Integer, Tbl_Exercise) where the key is the Exercise_Type_ID and the value is a collection of rows that are grouped by the key. To access them its pretty simple, you just have to think of the groupings as a sort of nested collection:

        Dim activityGroups = db.Tbl_Exercises.Where(Function(x) x.Exercise_Employee_ID = employeeId).GroupBy(Function(x) x.Exercise_Type_ID)
    
        ' Iterate through each collection of activities, grouped by activity types
        For Each group In activityGroups
    
            ' Iterate through each Tbl_Exercise in the group
            For Each activity In group
    
                ' Do something with an individual element
                Console.WriteLine(activity.Exercise_Duration)
    
            Next
    
        Next
    

    Obviously, you may want to do something different than print out individual collections, you may want to data bind each grouping to their own DataGrid so that you can display them separately. You may want to sort each grouping by date, or you may only want the first element from each group. Once you understand how the groupings are structured, it should be pretty easy to accomplish exactly what you’re looking for.

    Edit:

    If you’re just looking to aggregate some field in the grouping, this is very simple to accomplish without needing to resort to looping over the results. Simply adding a call to Select on top of your GroupBy will let you project the results into your desired aggregate form:

    Dim data = db.Tbl_Exercises _
                    .Where(Function(x) x.Exercise_Employee_ID = empId) _
                    .GroupBy(Function(x) x.Exercise_Type_Desc) _
                    .Select(Function(x)
                                Return New With {
                                    ' x.Key will give you access to the value that the grouping is grouped by
                                    .Exercise_Type_Desc = x.Key,
                                    .TotalDuration = x.Sum(Function(y) y.Exercise_Duration)
                                }
                            End Function)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear all i am using this function to get the GMT time, i am
I'm using this code for get all news: List<aspnet_News> allNews = context.aspnet_News.OrderByDescending(i => i.NewsId).ToList();
How do I get all of the columns using linq from this dictionary. Dictionary<int,
To get all contacts I'm using ABAddressBookCopyArrayOfAllPeople method but, this method return all contacts
I periodically get this problem where all of a sudden mako is using old
How can I get all the records from the index using Sphinx? Just like
i have a table called properties, i want to get all records in this
I'm using this to get the path and executable of default web browser: public
I'm using this to get random char from the string pool: $charset=str_shuffle('./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'); $charsetLength=strlen($charset)-1; $chosen=$charset[rand(0,$charsetLength)];
When using this user.meta.logins.$inc(); I get this error in the console: Mongoose MongooseNumber#$inc /

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.