Fluent NHibernate - Session.QueryOver中属性的列名

时间:2013-08-01 20:07:44

标签: c# nhibernate fluent-nhibernate nhibernate-mapping queryover

我有一个Session.QueryOver,我需要维护,我想删除列名的神奇字符串,如:


return Session.QueryOver<T>()
    .SelectList(list => list
        .Select(Projections.SqlGroupProjection(
            "CANDIES(" + MagicalStringForColumnName +") As [Candies]",
            "CANDIES(" + MagicalStringForColumnName + ")",
            new[] { "Candies" },
            new IType[] { NHibernateUtil.Int32 }))

我希望它像:

return Session.QueryOver<T>()
    .SelectList(list => list
        .Select(Projections.SqlGroupProjection(
            "CANDIES(" + Session.GetColumnNameFromMappedProperty(propInfo.Name) +") As [Candies]",
            "CANDIES(" + Session.GetColumnNameFromMappedProperty(propInfo.Name) + ")",
            new[] { "Candies" },
            new IType[] { NHibernateUtil.Int32 }))

1 个答案:

答案 0 :(得分:1)

事实证明,通过使用别名和SqlFunction,可以使用表达式而不是魔法字符串。

        object a = null;

        return Session.QueryOver<T>()
               .SelectList(l =>
                   l.Select(
                   Projections.GroupProperty(
                   Projections.SqlFunction("CANDIES", NHibernateUtil.Int32, Projections.Property(expression))
                   )).WithAlias(() => a)