我有以下查询:
var result = _session.QueryOver<Entity>()
.Where(e => e.Property == value)
.SelectList(list => list
.Select(f => Projections.Concat("prefix-", e.BigIntProperty)).WithAlias(() => alias.Whatever)
...
)
.TransformUsing(Transformers.AliasToBean<Model>())
.Future<Model>();
问题是
Projections.Concat()
只接受字符串,因为 e.BigIntProperty
不是,上面的不能编译。有没有办法投e.BigIntProperty
串?我尝试了以下类似的方法,但也不起作用:
.Select(f => Projections.Concat("prefix-", Projection.Cast(NHibernateUtil.String, e.BigIntProperty))).WithAlias(() => alias.Whatever)
, 自
Projections.Cast
返回 IProjection
而不是字符串。
最佳答案
Projections.Cast
似乎非常有限,因为它不能随意使用 Projection
s。幸运的是,您可以轻松创建自己的自定义投影,使您能够做到这一点:
public static class CustomProjections
{
public static IProjection Concat(params IProjection[] projections)
{
return Projections.SqlFunction(
"concat",
NHibernateUtil.String,
projections);
}
}
然后,您就可以使用您的
CustomProjections
像这样的类:var result = _session.QueryOver<Entity>()
.Where(e => e.Property == value)
.SelectList(list => list
.Select(CustomProjections.Concat(
Projections.Constant("prefix-"),
Projections.Cast(
NHibernateUtil.String,
Projections.Property<Entity>(e => e.BigIntProperty))))
.WithAlias(() => alias.Whatever)
...
)
.TransformUsing(Transformers.AliasToBean<Model>())
.Future<Model>();
关于nhibernate - 在 Concat 投影中使用 Cast Projection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26831677/