如何使用 Fluent NHibernate 映射通过子属性对 HasMany 集合进行排序

发布于 2024-09-05 03:22:33 字数 1689 浏览 3 评论 0原文

我正在使用 Fluent NHibernate 映射以下类:

public abstract class DomainObject
{
    public virtual int Id { get; protected internal set; }
}

public class Attribute
{
    public virtual string Name { get; set; }
}

public class AttributeRule
{
    public virtual Attribute Attribute { get; set; }
    public virtual Station Station { get; set; }
    public virtual RuleTypeId RuleTypeId { get; set; }
}

public class Station : DomainObject
{
    public virtual IList<AttributeRule> AttributeRules { get; set; }

    public Station()
    {
        AttributeRules = new List<AttributeRule>();
    }
}

我的 Fluent NHibernate 映射如下所示:

    public class AttributeMap : ClassMap<Attribute>
    {
        public AttributeMap()
        {
            Id(o => o.Id);
            Map(o => o.Name);
        }
    }

    public class AttributeRuleMap : ClassMap<AttributeRule>
    {
        public AttributeRuleMap()
        {
            Id(o => o.Id);
            Map(o => o.RuleTypeId);
            References(o => o.Attribute).Fetch.Join();
            References(o => o.Station);
        }
    }

    public class StationMap : ClassMap<Station>
    {
        public StationMap()
        {
            Id(o => o.Id);
            HasMany(o => o.AttributeRules).Inverse();
        }
    }

我想通过 Attribute.Name 属性对 Station 上的 AttributeRules 列表进行排序,但执行以下操作不起作用:

   HasMany(o => o.AttributeRules).Inverse().OrderBy("Attribute.Name");

我还没有找到方法尚未在映射中执行此操作。我可以创建一个 IQuery 或 ICriteria 来为我执行此操作,但理想情况下,我只想在我要求时对 AttributeRules 列表进行排序。

关于如何进行此映射有什么建议吗?

I am using Fluent NHibernate to map the following classes:

public abstract class DomainObject
{
    public virtual int Id { get; protected internal set; }
}

public class Attribute
{
    public virtual string Name { get; set; }
}

public class AttributeRule
{
    public virtual Attribute Attribute { get; set; }
    public virtual Station Station { get; set; }
    public virtual RuleTypeId RuleTypeId { get; set; }
}

public class Station : DomainObject
{
    public virtual IList<AttributeRule> AttributeRules { get; set; }

    public Station()
    {
        AttributeRules = new List<AttributeRule>();
    }
}

My Fluent NHibernate mappings look like this:

    public class AttributeMap : ClassMap<Attribute>
    {
        public AttributeMap()
        {
            Id(o => o.Id);
            Map(o => o.Name);
        }
    }

    public class AttributeRuleMap : ClassMap<AttributeRule>
    {
        public AttributeRuleMap()
        {
            Id(o => o.Id);
            Map(o => o.RuleTypeId);
            References(o => o.Attribute).Fetch.Join();
            References(o => o.Station);
        }
    }

    public class StationMap : ClassMap<Station>
    {
        public StationMap()
        {
            Id(o => o.Id);
            HasMany(o => o.AttributeRules).Inverse();
        }
    }

I would like to order the AttributeRules list on Station by the Attribute.Name property, but doing the following does not work:

   HasMany(o => o.AttributeRules).Inverse().OrderBy("Attribute.Name");

I have not found a way to do this yet in the mappings. I could create a IQuery or ICriteria to do this for me, but ideally I would just like to have the AttributeRules list sorted when I ask for it.

Any advice on how to do this mapping?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2024-09-12 03:22:33

我认为 OrderBy 方法接受它插入到生成的 SQL 子句中的字符串。所以只需执行

HasMany(o => o.AttributeRules).Inverse().OrderBy("Name");

其中“名称”是包含属性名称的列的名称。它应该位于列列表中,因为属性已连接到 AttributeRule。

你用其他方法解决了吗?请分享。

I think the OrderBy-method takes in the string that it inserts to the generated SQL-clause. So just doing

HasMany(o => o.AttributeRules).Inverse().OrderBy("Name");

Where the "Name" is the name of the column that contains Attribute's name. It should be in the column list because Attribute is joined to the AttributeRule.

Did you solve this other way? Please share.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文