1

我在框架 4.0 上。这是一个 WPF 应用程序,在 sql server CE 上,有点限制。我有一个看起来像这样的实体:

    public class TimeEvent
    {
       public int Id
       {get; set;}
       public DateTime EventDate
       {get; set;}
       public bool CheckIn
       {get; set;}
    }

我有两种类型的事件,一种用于签入(CheckIn为真),一种用于结帐(CheckIn为假)。每个事件每天发生一次。我想要使​​用 linq to entity 做的是最终得到一组像这样的对象:

public class Diff
{
   public DateTime Date //The date of both events
   {get; set;}
   public DateTime CheckInTime //Time of first event
   {get; set;}
   public DateTime CheckOutTime //Time of second event
   {get; set;}
   public int Hours //Difference in hours.
   { get { return (CheckOutTime - CheckInTime).Hours;} 
}

有适当的验证规则,因此一天内每种类型的事件不会超过一个。我尝试使用聚合函数,但我真的一无所获。谢谢!

4

3 回答 3

2

EventDate按日期部分对您的时间事件进行分组。为此,您需要使用EntityFunctions.TruncateTime方法(简单EventDate.Date不适用于实体框架)。

var query = from t in db.TimeEvents
            group t by EntityFunctions.TruncateTime(t.EventDate) into g
            let checkIn = g.Min(x => x.EventDate)
            let checkOut = g.Max(x => x.EventDate)           
            select new Diff {
                Date = g.Key,
                CheckInTime = checkIn,
                CheckOutTime = checkOut                    
            };

您也可以在服务器端使用EntityFunctions.DiffHours(checkOut, checkIn).

于 2013-01-16T07:38:42.073 回答
0

我想你有一个TimeEvents( List<TimeEvent>) 的列表,我会认为它是timeEvents.

我所有的代码都在下面给出。

    List<TimeEvent> timeEvents = new List<TimeEvent>();

                    //dummy values.
                    DateTime now = DateTime.Now;
                    for (int i = 0; i < 8; i++)
                    {
                        timeEvents.Add(new TimeEvent() { Id = i, EventDate = now, CheckIn = false });
                        now = now.Add(TimeSpan.Parse("3:1"));
                        timeEvents.Add(new TimeEvent() { Id = i, EventDate = now, CheckIn = true });
                        now = now + TimeSpan.Parse("1");
                    }

                    //group by 
                    var dataSet = timeEvents.GroupBy(a => a.EventDate.Date);
                    List<Diff> diffs = new List<Diff>();
                    foreach (var x in dataSet) 
                    {
                        if (!(x.Count() == 1))
                        {
                            diffs.Add(new Diff() { Date = x.ElementAt(0).EventDate.Date, CheckInTime = (x.ElementAt(0).CheckIn == true) ? x.ElementAt(0).EventDate : x.ElementAt(1).EventDate, CheckOutTime = (x.ElementAt(0).CheckIn == false) ? x.ElementAt(0).EventDate : x.ElementAt(1).EventDate });

                        }

                    }

                    //diffs list is full. now you can use the diffs as you like :)
于 2013-01-16T08:46:09.370 回答
0

无论您创建什么聚合委托,EF 都需要将其转换为 SQL。即使您的数据非常干净且有序,这也将是一个挑战。为什么不直接使用这个。

static IEnumerable<Diff> GetDiffs(YourContext context)
{
    bool? checkedIn;
    DateTime lastDate;
    foreach (var event in context.TimeEvents.OrderBy(te => te.EventDate))
    {
        if ((checkedIn.HasValue && checkedIn.Value = !event.CheckIn) ||
                (!checkedIn.HasValue && event.CheckIn))
        {
            if (checkedIn.HasValue && !event.CheckIn)
            {
                yield return new Diff
                    {
                        CheckInTime = lastDate;
                        CheckOutTime = event.EventDate;
                    };
            }                    

            checkedIn = event.CheckIn;
            lastDate = event.EventDate;
        }
    }
}

这样,您的数据从哪里开始或它是否正确触发都无关紧要。我也忽略了你的虚假Diff.Date财产。它不处理签入期间桥接日边界。

于 2013-01-16T10:36:34.297 回答