鉴于课程:
public class VotingResult : DomainEntity
{
public const string DefaultTag = "overall";
public VotingResult()
{
Dimension = DefaultTag;
}
public virtual DomainEntity Item { get; set; }
public virtual String Dimension { get; set; }
public virtual double Value { get; set; }
public virtual int VoteCount { get; set; }
public virtual DateTime DateCreated {get; set;}
}
和映射
public sealed class VotingResultMap : ClassMap<VotingResult>
{
private VotingResultMap()
{
Id(x => x.Id);
Map(x => x.Option);
Map(x => x.Dimension);
Map(x => x.DateCreated);
Map(x => x.Value);
Map(x => x.VoteCount);
ReferencesAny(x => x.Item).AddMetaValues(); // extension method to add discriminators
}
}
有没有办法投影VotingResult.Item.GetType(),通过Linq或HQL(“从VotingResult选择r.class”似乎不起作用。我可以使用一些讨厌的技巧,但我需要能够轻松地将引用扁平化为类型和id - 理想情况下使用Linq或AliasToBeanResultTransformer。
对于记录,DomainEntity子类的继承映射是Table Per Class。
由于
答案 0 :(得分:0)
使用LINQ,您可以在加载后进行投影
var results = Session.Query<VotingResult>()
.Where( ...)
.Include(result => result.Item)
.AsEnumerable()
.Select(result => new
{
ItemType = result.Item.GetType().Name,
ItemId = result.Item.Id
});