【问题标题】:How can I provide values for non-grouped columns in NHibernate?如何为 NHibernate 中的非分组列提供值?
【发布时间】:2010-03-17 13:06:44
【问题描述】:

我有一个条件查询:

Session.CreateCriteria<Sell043Report>()
  .SetProjection(.ProjectionList()
    .Add(LambdaProjection.GroupProperty<Sell043Report>(r => r.location))
    .Add(LambdaProjection.GroupProperty<Sell043Report>(r => r.agent))
    .Add(LambdaProjection.GroupProperty<Sell043Report>(r => r.cusip))
    .Add(LambdaProjection.GroupProperty<Sell043Report>(r => r.SettlementDate))
    .Add(LambdaProjection.GroupProperty<Sell043Report>(r => r.salePrice))
    .Add(LambdaProjection.GroupProperty<Sell043Report>(r => r.foreignFx))
    .Add(LambdaProjection.GroupProperty<Sell043Report>(r => r.batchNumber))
    .Add(LambdaProjection.GroupProperty<Sell043Report>(r => r.origSaleDate))
    .Add(LambdaProjection.GroupProperty<Sell043Report>(r => r.planName))
    .Add(LambdaProjection.GroupProperty<Sell043Report>(r => r.dateTimeAdded))
    .Add(LambdaProjection.Sum<Sell043Report>(r => r.shares))
    .Add(LambdaProjection.Sum<Sell043Report>(r => r.netMoney))
    .Add(LambdaProjection.Sum<Sell043Report>(r => r.grossMoney))
    .Add(LambdaProjection.Sum<Sell043Report>(r => r.taxWithheld))
    .Add(LambdaProjection.Sum<Sell043Report>(r => r.fees)))
  .List<Sell043Report>();

生成以下 SQL:

SELECT 
  this_.location as y0_, 
  this_.agent as y1_, 
  this_.cusip as y2_, 
  this_.SettlementDate as y3_, 
  this_.salePrice as y4_, 
  this_.foreignFx as y5_, 
  this_.batchNumber as y6_, 
  this_.origSaleDate as y7_, 
  this_.planName as y8_, 
  this_.dateTimeAdded as y9_, 
  sum(this_.shares) as y10_, 
  sum(this_.netMoney) as y11_, 
  sum(this_.grossMoney) as y12_, 
  sum(this_.taxWithheld) as y13_,
  sum(this_.fees) as y14_ 
FROM 
  MIS_IPS_Sell043Report this_ 
GROUP BY
  this_.location,
  this_.agent,
  this_.cusip,
  this_.SettlementDate,
  this_.salePrice,
  this_.foreignFx,
  this_.batchNumber,
  this_.origSaleDate,
  this_.planName,
  this_.dateTimeAdded

但是 Sell043Report 表的列比 SELECT 语句中列出的列多,所以我在尝试获取 Sell043Reports 列表时收到此错误:

System.ArgumentException: The value "System.Object[]" is not of type "xyz.Sell043Report" and cannot be used in this generic collection.

我怀疑问题在于我没有选择 Sell043Report 的所有列,因此它不知道如何将数据集映射到对象。我正在尝试实现这样的目标:

SELECT 
  this_.location as y0_, 
  this_.agent as y1_, 
  this_.cusip as y2_, 
  this_.SettlementDate as y3_, 
  this_.salePrice as y4_, 
  this_.foreignFx as y5_, 
  this_.batchNumber as y6_, 
  this_.origSaleDate as y7_, 
  this_.planName as y8_, 
  this_.dateTimeAdded as y9_, 
  sum(this_.shares) as y10_, 
  sum(this_.netMoney) as y11_, 
  sum(this_.grossMoney) as y12_, 
  sum(this_.taxWithheld) as y13_,
  sum(this_.fees) as y14_,
  '' as Address1,
  '' as Address2 // etc
FROM 
  MIS_IPS_Sell043Report this_ 
GROUP BY
  this_.location,
  this_.agent,
  this_.cusip,
  this_.SettlementDate,
  this_.salePrice,
  this_.foreignFx,
  this_.batchNumber,
  this_.origSaleDate,
  this_.planName,
  this_.dateTimeAdded

如何使用 NHibernate 做到这一点?

【问题讨论】:

    标签: sql nhibernate group-by


    【解决方案1】:

    在调用 .List() 之前,您可能想要这样的东西:

    SetResultTransformer(Transformers.AliasToBean(typeof(Result)))

    其中 Result 是一个新类,用于存储所需列的子集作为结果。

    另请参阅此问题:

    Projections in NHibernate

    你得到的错误是因为没有调用 SetResultTransformer,它想要返回一个对象数组。然后,您(无意中)尝试将该对象数组转换为 Sell043Report 对象列表,这是不可能的。

    您的另一个选择是删除对 List() 的调用,而是处理对象数组。

    【讨论】:

    • @Michael 如果可以的话,我会给你更多积分。 =) 谢谢!我从来没有听说过这些变压器。看来我错过了 NHibernate 的整个部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 2010-11-10
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    相关资源
    最近更新 更多