NHibernate 投影帮助
我在为我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,你的问题有两个部分。
1. 我的 DetachedCriteria 应该是什么样子?
如果您不执行任何其他聚合,
GROUP BY
应提供与DISTINCT
相同的结果。 这是我将使用的查询:2. 如何使用 Castle ActiveRecord 执行它?
我从未使用过 ActiveRecord,但根据方法签名,我希望这样的事情能够工作:
如果您有权访问 NHibernate 会话,您也可以这样执行它:
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 asDISTINCT
. This is the query I would use: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:
If you have access to the NHibernate session, you could also execute it this way: