0

我有这个模型:

Public Class Tbl_Exercise

    <Key()> Public Property Exercise_ID() As Integer
    Public Property Exercise_Employee_ID() As Integer
    Public Property Exercise_Create_Date() As Date
    <ForeignKey("Tbl_Exercise_Type")> _
    Public Property Exercise_Type_ID() As Integer
    Public Property Exercise_Duration() As Integer

    Public Overridable Property Tbl_Exercise_Type As Tbl_Exercise_Type

End Class

我需要得到Exercise_Duration一年中每周的总和。然后我需要检查一周的总和是否大于或等于150. 如果是,我需要 +1 另一个变量(计数)。目标是显示这个:

# of weeks you've reached 150: X out of Z

(其中X周数大于或等于 150 且Z等于当年的总周数。)

最终的

    ' get number of weeks the exercise goal was reached (greater than or equal to the goal)
    Dim exerciseDb = New ExerciseDbContext
    Dim exercise = exerciseDb.Tbl_Exercises.Where(Function(x) x.Exercise_Employee_ID = empId)


    Dim weeks = exercise.ToList.GroupBy(Function(x) CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(x.Exercise_Create_Date, CalendarWeekRule.FirstDay, DayOfWeek.Sunday))

    Dim totalWeeks = 0

    For Each week In weeks

        Dim sum = week.Sum(Function(x) x.Exercise_Duration)

        If sum > 150 Then

            totalWeeks += 1

        End If

    Next

    Debug.Print("over150: " + totalWeeks.ToString)
4

2 回答 2

2
using System.Globalization;

DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
Calendar cal = dfi.Calendar;

var recap = 
    (from e in exercises
     group e by cal.GetWeekOfYear(e.Exercise_Create_Date, 
                                  dfi.CalendarWeekRule, 
                                  dfi.FirstDayOfWeek) 
     into g
     select new 
     { 
        g.Key, 
        Total = g.Sum(x => x.Exercise_Duration) 
     } 
     into p 
     where p.Total > 150
     select p)
    .Count();
于 2012-09-26T17:45:57.470 回答
1

这是 C# 中的一个示例:

public class Exercise
{
    public DateTime CreateDate { get; set; }
    public int Duration { get; set; }
}

class Program
{
    static void Main()
    {
        Exercise[] ex = new Exercise[]
        {
            new Exercise { CreateDate = DateTime.Parse("1/1/2012"), Duration = 160 },
            new Exercise { CreateDate = DateTime.Parse("1/8/2012"), Duration = 160 },
            new Exercise { CreateDate = DateTime.Parse("1/15/2012"), Duration = 160 },
            new Exercise { CreateDate = DateTime.Parse("2/1/2012"), Duration = 100 },
            new Exercise { CreateDate = DateTime.Parse("3/1/2012"), Duration = 75 },
            new Exercise { CreateDate = DateTime.Parse("3/1/2012"), Duration = 80 }
        };

        var weeks = ex.GroupBy(x => CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(x.CreateDate, CalendarWeekRule.FirstDay, DayOfWeek.Sunday));
        int currentweek = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
        int over150 = weeks.Where(group => group.Sum(item => item.Duration) > 150).Count();
        Console.WriteLine(String.Format("# of weeks you've reached 150: {0} out of {1}", over150, currentweek));
    }

}
于 2012-09-26T17:44:20.363 回答