4

考虑以下QueryOver(季度和中心是传入的变量):

QueryOver.Of<Activity>()
        .Where(Restrictions.On<Activity>(a => a.StartDate).IsBetween(quarter.StartDate).And(quarter.EndDate) ||
               Restrictions.On<Activity>(a => a.EndDate).IsBetween(quarter.StartDate).And(quarter.EndDate) ||
               Restrictions.And(Restrictions.Lt("StartDate", quarter.StartDate), Restrictions.Gt("EndDate", quarter.EndDate)))  //TODO: Refactor this to remove magic strings
        .And(a => a.Centre == centre)
        .OrderBy(a => a.Title).Asc;

此查询完美运行,但我想更改以下限制以删除魔术字符串:

Restrictions.And(Restrictions.Lt("StartDate", quarter.StartDate), Restrictions.Gt("EndDate", quarter.EndDate))

以下是实体(为简洁起见):

public class Quarter
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }

}

public class Activity
{
    public string Title { get; set; }
    public Centre Centre { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }

}

public class Centre
{
    public string Name { get; set; }
}

那么使用新的 QueryOver 扩展的类型安全方法是什么,可以让我删除这些魔术字符串?

4

1 回答 1

8

这就是你想要的:

Restrictions.And(
    Restrictions.Lt(Projections.Property<Activity>(x => x.StartDate),
                    quarter.StartDate),
    Restrictions.Gt(Projections.Property<Activity>(x => x.EndDate),
                    quarter.EndDate)))

旁注:属性名称不是魔术字符串。

于 2011-06-02T13:31:01.157 回答