2012-09-21 5 views
0
내가 날짜순으로 내 모든 기록을 얻을이 사용하고

이과를 통해LINQ GROUPBY 사용

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

I 루프 :

For Each i As Tbl_Exercise In myData 

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

    Next 

을 어떻게 i.Tbl_Exercise_Type.ExType_Desc 그룹화 그래서 내가하지 할 수있는 중복이 있습니까? 내 루프 내에서 변수에 액세스하는 방법을 모른다,

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

그러나 :

나는이 시도.

감사합니다.

편집 :

<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 

편집 :

' 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 
+0

하지만 예 : 간단 GroupBy 위에 Select에 전화를 추가하면 원하는 집합 형태로 결과를 투영하게됩니다 'Exercise_Type_ID'를 사용합니다. 어느 그룹으로 그룹을 찾고 있습니까? – user7116

+0

'ExType_Desc'는'Tbl_Exercise_Type'에 속하며'i.Tbl_Exercise_Type.ExType_Desc'를 통해 액세스됩니다. – user1477388

+0

각 직원이 자신이 수행하는 활동 유형별로 그룹화 된 운동 기간 컬렉션을 얻으 려하고 있기 때문에 이해합니다. – mclark1129

답변

1

참고 : 귀하의 예를 완전히 명확하지 않다, 그래서 무엇에 대한 몇 가지 가정을거야 당신은 내 해결책으로하려고하고 있습니다. 더 많은 세부 정보가 제공되면 필요에 따라 수정하겠습니다.

각 직원은 수행 한 운동 유형 모음을 얻으 려하고 있으며 각 운동 유형마다 수행 한 시간의 모음을 원합니다. 이러한 활동. 내가 Tbl_Exercises의 행이 같다고 가정하는거야

:

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 
가 만든 후에는 마지막에 만드는 시도가 아주 가까이

, 그냥 실제로 그룹화를 사용할 필요가 그것.

GroupBy 메서드가 반환 한 내용은 실제로 IGrouping(Of Integer, Tbl_Exercise)이고 여기에서 키는 Exercise_Type_ID이고 값은 키로 그룹화 된 행 모음입니다. 그들은 아주 간단합니다, 당신은 단지 중첩 된 집합의 일종으로 그룹화 생각해야 액세스하려면 :

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 

물론, 개별 컬렉션을 인쇄와는 다른 무언가를 할 수 있습니다, 당신은 데이터 바인딩 할 수 있습니다 각 그룹을 자신의 DataGrid으로 그룹화하여 개별적으로 표시 할 수 있습니다. 각 그룹을 날짜순으로 정렬하거나 각 그룹의 첫 번째 요소 만 필요할 수도 있습니다. 일단 그 그룹이 어떻게 구조화되는지를 이해하면, 당신이 찾고있는 것을 정확히 성취 할 수 있어야합니다.

편집 :

방금 ​​그룹의 일부 필드를 집계 찾고 있다면, 이것은 결과를 통해 반복에 의존 할 필요없이 달성하기 매우 간단합니다.나는, 당신은 당신이 ExType_Desc``에 의해 그룹화 할 혼란 말할 것 같아

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

고맙다. 나는 이것을 월요일에 시험 할 것이다. 내가 찾고 있던 대답은 그룹을 반복해야하는 부분에있는 것처럼 보입니다. – user1477388

+0

효과가없는 것 같습니다. 'activity.Tbl_Exercise_Type.ExType_Desc'에있는 항목들은 여전히 ​​중복되어 나열되어있어 그룹화 된 것으로 보이지 않습니다. 나는 나의 모든 행동으로 나의 질문을 편집했다. – user1477388

+0

이것은 http://postimage.org/image/ejuh6jeex/의 일례입니다. 쿼리를 'Dim activityGroups = db.Tbl_Exercises.Where (Function (x) x.Exercise_Employee_ID = empId) .GroupBy (Function (x) x.Tbl_Exercise_Type.ExType_Desc)'로 변경했습니다. 당신의 도움을 주셔서 감사합니다. – user1477388