【问题标题】:Create query "sum" multiple columns in Nhibernate在 Nhibernate 中创建查询“sum”多个列
【发布时间】:2016-11-08 10:37:23
【问题描述】:

需要使用Query Over (Nhibernate) C# 创建一个查询,以添加多个列。纯sql示例:

SELECT SUM(col1 + col2 + col3 + col4)
FROM tabela

首先我是这样做的:

Table table = null;
Session.QueryOver<Table>(() => tabela)
       .Select(Projections.Sum<Table>(t => t.col1))
       .Select(Projections.Sum<Table>(t => t.col2))
       .Select(Projections.Sum<Table>(t => t.col3))
       .Select(Projections.Sum<Table>(t => t.col4))

但是这样每列并生成 4 列,将全部相加并仅生成一列。

【问题讨论】:

    标签: c# jquery sql nhibernate


    【解决方案1】:

    更简单:

    Table table = null;
    Session.QueryOver<Table>(() => tabela)
           .Select(Projections.Sum<Table>(t => t.col1 + t.col2 + t.col3 + t.col4))
    

    【讨论】:

    • 我喜欢这样,不要放置超过一列。显示异常:Message = the variable 't' type Table' is referenced in the scope' ',但未设置。
    • @MarcosVinicius 你的cols是什么类型的?可以为空吗?
    • 我使用十进制?,可以为空。都是同一类型。
    • @MarcosVinicius 这可能是问题所在。只需检查它为 null
    • 我是这样做的,但遇到了同样的问题。
    猜你喜欢
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 2019-09-22
    • 1970-01-01
    • 2020-06-11
    相关资源
    最近更新 更多