NHibernate 投影帮助

发布于 2024-07-21 10:20:05 字数 1080 浏览 4 评论 0原文

我在为我的 nhibernate detachedcriteria 对象创建投影时遇到问题。

我有一个 Spa 类,它链接到表地址。

地址有一个名为“城市”的字段,它是一个字符串。

public class Spa : IAggregateRoot
{
       [BelongsTo("AddressID", Cascade = CascadeEnum.All)]
       public Address Address { get; set; }
}

我的最终目标是获得一份清晰的城市名称列表。

如果我能获得不同城市的所有水疗中心,我也会很高兴。

我所有的尝试都是徒劳的,也没有找到任何有用的帖子。

到目前为止我已经尝试过:

DetachedCriteria query = DetachedCriteria.For<Spa>()
                         .CreateAlias("Address", "A")

query.SetProjection(
         Projections.Distinct(Projections.ProjectionList()
         .Add(Projections.Alias(Projections.Property("Address"), "A"))));

var Spas = ActiveRecordMediator<Spa>.FindAll(query);

我知道上面的内容是不正确的,只是想找到一个开始的地方。

任何帮助,将不胜感激。 此外,任何简单的投影教程都会受到赞赏,但似乎无法直接找到任何东西。

我也尝试过,但出现了强制转换错误,调查了一下:

DetachedCriteria query = DetachedCriteria.For<Spa>() 
.CreateAlias("Address", "A")
.SetProjection(Projections.Distinct(Projections.Property("A.City"))); 

Im having a problem creating a projection for my nhibernate detachedcriteria object.

I have a class Spa which is linked to table Address.

Address has a field called City which is a string.

public class Spa : IAggregateRoot
{
       [BelongsTo("AddressID", Cascade = CascadeEnum.All)]
       public Address Address { get; set; }
}

My ultimate goal is to get a distinct list of City names.

If i could get all spas with distinct cities i would be happy too.

All my attempts have been for naught and havent found any helpful posts.

So far i've tried:

DetachedCriteria query = DetachedCriteria.For<Spa>()
                         .CreateAlias("Address", "A")

query.SetProjection(
         Projections.Distinct(Projections.ProjectionList()
         .Add(Projections.Alias(Projections.Property("Address"), "A"))));

var Spas = ActiveRecordMediator<Spa>.FindAll(query);

I know the above is not correct, just trying to find somewhere to start.

Any help would be appreciated.
Also any simple projections tutorials would be appreciated, cant seem to find anything straight forward out there.

I also tried, but got cast error, looking into it:

DetachedCriteria query = DetachedCriteria.For<Spa>() 
.CreateAlias("Address", "A")
.SetProjection(Projections.Distinct(Projections.Property("A.City"))); 

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

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

发布评论

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

评论(1

固执像三岁 2024-07-28 10:20:05

在我看来,你的问题有两个部分。

1. 我的 DetachedCriteria 应该是什么样子?

如果您不执行任何其他聚合,GROUP BY 应提供与 DISTINCT 相同的结果。 这是我将使用的查询:

var query = DetachedCriteria.For<Spa>()
    .CreateAlias("Address", "A")
    .SetProjection(Projections.GroupProperty("A.City"));

2. 如何使用 Castle ActiveRecord 执行它?

我从未使用过 ActiveRecord,但根据方法签名,我希望这样的事情能够工作:

var cities = ActiveRecordMediator<string>.FindAll(query);

如果您有权访问 NHibernate 会话,您也可以这样执行它:

var cities = query.GetExecutableCriteria(session).List<string>();

It seems to me there are two parts to your question.

1. What should my DetachedCriteria look like?

If you are not performing any other aggregations, GROUP BY should provide the same results as DISTINCT. This is the query I would use:

var query = DetachedCriteria.For<Spa>()
    .CreateAlias("Address", "A")
    .SetProjection(Projections.GroupProperty("A.City"));

2. How do I execute it with Castle ActiveRecord?

I have never used ActiveRecord, but based on the method signatures, I would expect something like this to work:

var cities = ActiveRecordMediator<string>.FindAll(query);

If you have access to the NHibernate session, you could also execute it this way:

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