高级LINQ / Lambda表达式

时间:2012-09-26 17:26:51

标签: vb.net linq lambda

我有这个型号:

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)

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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));
    }

}