2010-03-17 2 views
1

У меня есть критерии запроса:Как я могу предоставить значения для негрупповых столбцов в NHibernate?

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 имеет дополнительные столбцы, чем те, которые перечислены в ЗЕЬЕСТ, так что я получаю эту ошибку когда пытаясь получить список Sell043Reports:

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

Я подозреваю, что проблема в том, что я не выбрать все колонки s для 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?

ответ

3

Вы, вероятно, хотите что-то вроде этого перед вызовом .list <>():

SetResultTransformer (Transformers.AliasToBean (TypeOf (Результат)))

Где Результат новый класс, который используется для сохраните подмножество столбцов, которые вы хотите в результате.

Смотрите также вопрос:

Projections in NHibernate

Ошибка вы получаете это потому, что без обращения к SetResultTransformer, он хочет вернуть массив объектов. Затем вы (непреднамеренно) пытаетесь передать этот массив объектов в список объектов Sell043Report, что невозможно.

Другой вариант - удалить вызов в список <>() и вместо этого обработать массив объектов.

+0

@ Майкл. Я бы дал вам больше очков, если бы смог. =) Спасибо! Я никогда не слышал об этих трансформаторах. Кажется, я пропустил целый раздел NHibernate. – ddc0660

Смежные вопросы