0

我不是一个 LINQ 程序员,所以这让我有些困惑。我有两个表:第一个是带有 StartDate 和 EndDate 以及 ID 的 Schedule;其次,带有 InstanceDate 和 ScheduleID 的 ScheduleInstance 表。对于 Schedule.StartDate 和 Schedule.EndDate 之间的每一天,我需要创建一个 ScheduleInstance 行 - 但是,只提前 24 小时。由于计划是在 24 小时前瞻内创建和删除的,因此我必须每 n 分钟生成一个检查器,以检查 ScheduleID 的 ScheduleInstance 是否存在于该 24 小时窗口内。

楷模:

public class Schedule
{
    public int ID { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}
public class ScheduleInstance
{
    public int ID { get; set; }
    public int ScheduleID { get; set; }
    public DateTime InstanceDate { get; set; }
}

LINQ的开始:

var skeds = from sked in context.Schedules
            join instance in context.ScheduleInstances
                on sked.ID equals instance.ScheduleID
            where ((sked.StartDate <= DateTime.Now) && (sked.EndDate >= DateTime.Now))
            select sked.ID;

(显然错了)

总而言之,我需要在接下来的 24 小时内获取 ScheduleInstance.InstanceDate 不存在的 Schedule.ID 列表。

非常感谢您的帮助。

更新

        DateTime tomorrow = DateTime.Now.AddDays(1);
        var skeds = from sked in context.Schedules
                    join instance in context.ScheduleInstances
                         on sked.ID equals instance.ScheduleID into g
                    where (sked.StartDate <= tomorrow) &&
                          (sked.EndDate >= tomorrow) &&
                           !g.Any()
                    select sked;

现在,如果明天不存在一个实例,则会创建一个实例(在与此处无关的后续代码中)。如果我将 StartDate 设置为 Now+2mins ,那么在 2 分钟后创建计划 - 完美。但是,如果我将时钟提前 24 小时,我应该会得到一大堆新实例。重申一下,如果计划开始日期是现在,结束日期是 30 天后,那么我应该以 31 个实例结束,每个新实例都提前 24 小时创建。

4

4 回答 4

1

我相信这会成功

var today = DateTime.Now;
var nextDay = today.AddDays(1);
var scheds = from sched in context.Schedules                        
             join instance in context.ScheduleInstances
                on sched.ID equals instance.ScheduleID into schedInstances
             where (sched.StartDate >= today) && 
                   (sched.EndDate <= nextDay) &&
                   !schedInstances.Any()
             select sched.ID;
于 2012-10-19T21:39:36.593 回答
1

因此,您想要日程表的 StartDate 和明天之间每天少于一个实例的日程表吗?以下是如何实现这一目标的粗略想法:

DateTime tomorrow = DateTime.Now.AddDays(1);
var skeds = from sked in context.Schedules
            join instance in context.ScheduleInstances
                 on sked.ID equals instance.ScheduleID into g
            let instances = g.Where(x => x.InstanceDate >= sked.StartDate &&
                                         x.InstanceDate <= tomorrow)
            where (sked.StartDate <= tomorrow) &&
                  (sked.EndDate >= tomorrow) &&
                  (instances.Count() < (tomorrow - sked.StartDate).Days)
            select sked;
于 2012-10-20T02:34:01.460 回答
0

试试这个:(未经测试)

var skeds = from sked in context.Schedules
            from instance in context.ScheduleInstances
            where (sked.ID == instance.ScheduleID) && ((sked.StartDate <= DateTime.Now) && sked.EndDate >= DateTime.Now))
            select sked.ID;
于 2012-10-19T21:39:43.010 回答
0

一方面,我应该采用 KISS 原则。另一方面,我有三个查询而不是一个。但它有效!

        var instances = from inst in context.ScheduleInstances
                        where (inst.StartDateTime >= DateTime.Now)
                        && (inst.StartDateTime < tomorrow)
                        select inst;

        var skeds = from sked in context.Schedules
                    where (sked.StartDate <= DateTime.Now)
                    && (sked.EndDate >= tomorrow)
                    select sked;

        var none = from sked in skeds
                   join inst in instances
                   on sked.ID equals inst.ScheduleID
                   into tg
                   from tcheck in tg.DefaultIfEmpty()
                   where tcheck == null
                   select sked;
于 2012-10-22T18:08:40.120 回答