1

我有一个简单的豁免模型,我想做一个查询,返回所有未被覆盖的豁免。

public class Waiver
{
    private readonly int id;

    protected Waiver()
    {
        this.id = 0;
    }

    public virtual int Id { get { return id; } }

    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual bool IsRequired { get; set; }
    public virtual DateTime EffectiveDate { get; set; }

    public virtual Waiver OverriddenWaiver { get; set; }
}

这是地图:

  <class name="Waiver" table="Music_Waivers">
    <id name="id" access="field" column="WaiverId" unsaved-value="0">
      <generator class="native" />
    </id>

    <property name="Name" column="Name" />
    <property name="Description" column="Description" />
    <property name="IsRequired" column="IsRequired" />
    <property name="EffectiveDate" column="EffectiveDate" />

    <many-to-one name="OverriddenWaiver" class="Waiver" column="OverrideWaiverId" />
  </class>

现在我想在我的存储库中有一个带有签名public IList GetLatest()的方法。出于某种原因,我很难用 CriteriaAPI 实现它。我可以用 T-SQL 写这个没问题。

4

1 回答 1

0

我最终暴力破解了一个解决方案。它不漂亮,但因为我知道表会很小(可能最终只有 5 行),所以我想出了以下代码解决方案:

public IList<Waiver> GetLatest()
{
    using (var session = SessionManager.OpenSession())
    {               
        var criteria = session.CreateCriteria(typeof (Waiver));
        var waivers = criteria.List<Waiver>();

        var nonOverridenWaivers = new List<Waiver>();

        foreach(var waiver in waivers)
        {
            bool overrideExists = waivers.Any(w => w.Overrides != null &&
                                                   w.Overrides.Id == waiver.Id);
            if (!overrideExists)
                nonOverridenWaivers.Add(waiver);
        }

        return nonOverridenWaivers;
    }
}
于 2009-05-27T20:16:12.250 回答