nHibernate - Adding aggregate functions in .select - nhibernate

I am trying to implement a business formula in QueryOver.
POorder.Estimate is a calculated field and I need to get
ToDateOrderAmount = POrder.Estimate - Sum(PODist.Field1) - Sum(PODisTaxRebate.Field1 + PODisTaxRebate.Field2)
So I need to write a query. What I have now is:
var reportModels =
Session.QueryOver<Domain.Model.Purchasing.Vendor>(() => v)
.Left.JoinQueryOver(() => v.Invoices, () => invoice)
.Left.JoinQueryOver(() => invoice.PurchaseOrder, () => poOrder)
.Left.JoinQueryOver(() => poOrder.PurchaseOrderDistributions, () => poDistribution)
.Left.JoinQueryOver(() => poDistribution.TaxRebate, () => poTaxRebate)
.SelectList(
list =>
list.Select(() => v.Number).WithAlias(() => varptModel.VendorNumber)
.Select(() => v.TypeCode.Code).WithAlias(() => varptModel.VendorType)
.Select(() => v.Name).WithAlias(() => varptModel.VendorName)
.Select(() => v.PurchasingContactPhoneNumber + "-Ext." + v.PurchasingContactPhoneNumberExt).WithAlias(() => varptModel.Phone)
.Select(() => v.Address).WithAlias(() => varptModel.Address)
.Select(() => invFiscalYear.Year).WithAlias(() => varptModel.Year)
.Select(() => invoice.TotalAmount).WithAlias(() => varptModel.InvoiceToDate)
.Select(() => invoice.AmountPaidToDate).WithAlias(() => varptModel.PaymentToDate)
.Select(() => poOrder.Estimate).WithAlias(() => varptModel.OrdersToDate)
.Select(() => poOrder.Estimate - Sum(poDistribution.Field1) - Sum(poTaxRebate.Discount1 + poTaxRebate.Discount2) )
).List();
But this is not right. What should I change it to?

I tried many things and found this working
.Select(Projections.SqlFunction(new VarArgsSQLFunction("", "+", ""),
NHibernateUtil.Double,
Projections.SqlFunction(new VarArgsSQLFunction("", "+", ""),
NHibernateUtil.Double,
Projections.Sum(Projections.SqlFunction("coalesce", NHibernateUtil.Double, Projections.Property(() => invoiceLineItem.Expense), Projections.Constant(0))),
Projections.Sum(Projections.SqlFunction("coalesce", NHibernateUtil.Double, Projections.Property(() => invitemTaxRebate.Rebate1Expense), Projections.Constant(0)))),
Projections.Sum(Projections.SqlFunction("coalesce", NHibernateUtil.Double, Projections.Property(() => invitemTaxRebate.Rebate2Expense), Projections.Constant(0)))))
.WithAlias(() => varptModel.ToDateInvoices)
which gave me this in SQL
sum(coalesce(invoicelin8_.Expense, 0 )) + sum(coalesce(invitemtax9_.Rebate1Expense, 0 )) + sum(coalesce(invitemtax9_.Rebate2Expense, 0 ))
I added Coalesce as when we add or subtract values with null value, all values becomes null in result. Just a hint for new ones.

Related

Yii cactivedataprovider count duplicates record and all record count

$criteria = new CDbCriteria;
$criteria->group = 't.blahah_id';
$result = new CActiveDataProvider('SocialnetworkSpams', array(
'criteria' => $criteria,
));
echo "<pre>";
print_r($result->getData());
Array
(
[0] => SocialnetworkSpams Object
(
[total] =>
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 1
[created_date] => 2015-12-04 03:50:47
[feedtype] => 1
[feedback] =>
[spam_status] => 1
[approveby_id] => 1
[blahah_id] => 1
[bunit_id] => 3
[spamby_id] => 1
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 1
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[1] => SocialnetworkSpams Object
(
[total] =>
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 4
[created_date] => 2015-12-04 04:24:07
[feedtype] => 5
[feedback] => a
[spam_status] => 0
[approveby_id] =>
[blahah_id] => 2
[bunit_id] => 3
[spamby_id] => 1
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 4
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
)
I want duplicate record get using blahah id you can check bellow image:
Thanks
I have been resolved this issue.
$criteria = new CDbCriteria;
$criteria->select = '*, count(*) as total';
$criteria->group = 't.blahah_id';
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
View Page :
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'spam-user-grid',
'itemsCssClass' => 'table table-striped table-bordered table-hover',
'dataProvider' => $model->search(),
'template' => "{items}{pager}",
'pager' => array(
'header' => '',
),
'columns' => array(
array(
'header' => 'Blahah',
'type' => 'raw',
'value' => 'ucfirst($data->ratingprofile->name)',
//'filter' => CHtml::textField('User[name]'),
),
array(
'header' => 'Total',
'type' => 'raw',
'value' => '$data->total',
),
),
)
));

NHibernate projection: How to create AliasToBean projection?

I am trying to convert this inefficient query into one that projects into a dto.
Original query looks like this:
var flatFeePolicies = _session.QueryOver<FlatChargeAccessFee>(() => flatChargeAccessFeeAlias)
.JoinAlias(x => x.AgreementAccessFee, () => agreementAccessFeeAlias)
.JoinQueryOver(x => x.ClientPolicy, () => clientPolicyAlias)
.Where(y => agreementAccessFeeAlias.Agreement.Id == request.AgreementId)
.List()
.Select(x => new FlatChargeAccessFeeInfo()
{
FlatChargeAccessFeeId = x.Id,
ClientName = x.ClientPolicy.Bid.Client.Name,
PolicyNumber = x.ClientPolicy.PolicyNumber,
ClientPolicyId = x.ClientPolicy.Id,
AgreementAccessFeeId = x.AgreementAccessFee.Id,
ShouldCheckBeGenerated = x.ShouldCheckBeGenerated,
MonthlyFee = x.MontlyFeeAmount.Amount.ToString(),
PolicyYear = x.ClientPolicy.PolicyNumber.Year
})
.ToList();
I tried it like this:
var flatFeePolicies = _session.QueryOver<FlatChargeAccessFee>(() => flatChargeAccessFeeAlias)
.JoinAlias(x => x.AgreementAccessFee, () => agreementAccessFeeAlias)
.JoinQueryOver(x => x.ClientPolicy, () => clientPolicyAlias)
.Where(y => agreementAccessFeeAlias.Agreement.Id == request.AgreementId)
.SelectList(list => list
.Select(x => x.Id).WithAlias(() => feeInfo.FlatChargeAccessFeeId)
.Select(x => x.ClientPolicy.Bid.Client.Name).WithAlias(() => feeInfo.ClientName)
.Select(x => x.ClientPolicy.PolicyNumber).WithAlias(() => feeInfo.PolicyNumber)
.Select(x => x.ClientPolicy.Id).WithAlias(() => feeInfo.ClientPolicyId)
.Select(x => x.AgreementAccessFee.Id).WithAlias(() => feeInfo.AgreementAccessFeeId)
.Select(x => x.ShouldCheckBeGenerated).WithAlias(() => feeInfo.ShouldCheckBeGenerated)
.Select(x => x.MontlyFeeAmount.Amount.ToString()).WithAlias(() => feeInfo.MonthlyFee)
.Select(x => x.ClientPolicy.PolicyNumber.Year).WithAlias(() => feeInfo.PolicyYear)
)
.TransformUsing(Transformers.AliasToBean<FlatChargeAccessFeeInfo>())
.List<FlatChargeAccessFeeInfo>();
and I am getting an error that variable "x" has been referenced in scope but was not defined. What is the proper syntax to convert this?
After help from Andrew, here is the correct version that works
ClientPolicy clientPolicyAlias = null;
Client clientAlias = null;
Bid bidAlias = null;
AgreementAccessFee agreementAccessFeeAlias = null;
FlatChargeAccessFee flatChargeAccessFeeAlias = null;
FlatChargeAccessFeeInfo feeInfo = null;
var flatFeePolicies = _session.QueryOver<FlatChargeAccessFee>(() => flatChargeAccessFeeAlias)
.JoinAlias(a => a.AgreementAccessFee, () => agreementAccessFeeAlias)
.JoinQueryOver(b => b.ClientPolicy, () => clientPolicyAlias)
.JoinAlias(b=>b.Bid,()=>bidAlias)
.JoinAlias(b=>b.Client, ()=>clientAlias)
.Where(c => agreementAccessFeeAlias.Agreement.Id == request.AgreementId)
.SelectList(list => list
.Select(d => d.Id).WithAlias(() => feeInfo.FlatChargeAccessFeeId)
.Select(e => clientAlias.Name).WithAlias(() => feeInfo.ClientName)
.Select(e => clientAlias.Number).WithAlias(() => feeInfo.ClientNumber)
.Select(f => bidAlias.OptionNumber).WithAlias(() => feeInfo.BidOptionNumber)
.Select(f => bidAlias.Year).WithAlias(()=>feeInfo.PolicyYear)
.Select(g => clientPolicyAlias.Id).WithAlias(() => feeInfo.ClientPolicyId)
.Select(h => agreementAccessFeeAlias.Id).WithAlias(() => feeInfo.AgreementAccessFeeId)
.Select(j => j.ShouldCheckBeGenerated).WithAlias(() => feeInfo.ShouldCheckBeGenerated)
.Select(k => k.MontlyFeeAmount.Amount).WithAlias(()=>feeInfo.MonthlyFee)
)
.TransformUsing(Transformers.AliasToBean<FlatChargeAccessFeeInfo>())
.List<FlatChargeAccessFeeInfo>();
You're close, a few things though:
This select:
.Select(x => x.MontlyFeeAmount.Amount.ToString()).WithAlias(() => feeInfo.MonthlyFee)
will not work. QueryOver attempts to turn your code directly into SQL. If the property does not exist as a column in the database, the query won't work properly (unless you're using a mapped custom type, QueryOver can handle those)
Nested property access won't work either:
.Select(x => x.ClientPolicy.Bid.Client.Name).WithAlias(() => feeInfo.ClientName)
for a similar reason listed above. QueryOver will attempt to turn your property access directly into SQL. You'll need to explicitly join from ClientPolicy to Bid to Client.
In general, remember that you're writing code that's going to be turned into SQL. In fact, I normally write the SQL I want to generate first and then write the QueryOver that corresponds to that. Hope that helps!

nHibernate many to many not getting list of child objects on retrieved object

// load the client and any many to one relationships
var clientRootQuery = session.QueryOver(() => clientAlias);
clientRootQuery.Left.JoinAlias(() => clientAlias.Person, () => personAlias)
.Left.JoinAlias(() => personAlias.SexType, () => sexTypeAlias)
.Left.JoinAlias(() => personAlias.EyeColorType, () => eyeColorTypeAlias)
.Left.JoinAlias(() => clientAlias.Organization, () => organizationAlias)
.Left.JoinAlias(() => clientAlias.ClientStatusType, () => clientStatusTypeAlias)
.Left.JoinAlias(() => clientAlias.Notes, () => noteAlias)
.Left.JoinAlias(() => noteAlias.Comments, () => commentAlias)
.Where(() => clientAlias.Id == clientId)
.Future<Client>();
// load the note collection into the nhibernate session
// todo ******************** this doesn't work. nhibernate is still firing off queries in the adapter fill method rather than using the values pulled here.
var notes = session.QueryOver(() => clientAlias)
.Left.JoinAlias(() => clientAlias.Notes, () => noteAlias)
.Left.JoinAlias(() => noteAlias.Comments, () => commentAlias)
.Where(() => clientAlias.Id == clientId)
.Future<Client>();
return clientRootQuery.Take(1).SingleOrDefault();
This does not return many addresses on the client object nor does it return many notes. This should work. There is a client with many notes and many addresses.
Any ideas?
the reason why clientAddresses are not initialized is because you set a filter on them so NHibernate assumes that not all clientAddresses are loaded to fully initialize the collection.
var clientRootQuery = session.QueryOver(() => clientAlias)
.Left.JoinAlias(() => clientAlias.Person, () => personAlias)
.Left.JoinAlias(() => personAlias.SexType, () => sexTypeAlias)
.Left.JoinAlias(() => personAlias.EyeColorType, () => eyeColorTypeAlias)
.Left.JoinAlias(() => clientAlias.Organization, () => organizationAlias)
.Left.JoinAlias(() => clientAlias.ClientStatusType, () => clientStatusTypeAlias)
.Where(() => clientAlias.Id == clientId)
.Future();
var notes = session.QueryOver(() => clientAlias)
.Left.JoinAlias(() => clientAlias.Notes, () => noteAlias)
.Where(() => noteAlias.Client.Id == clientId)
.Future();
var list = clientRootQuery.ToList();
return list.FirstOrDefault();
It needs to be a left join.

NHibernate Projections - How to project a value from within associations

I am trying to project a single value from an entity's association with no luck. Only want the city name from the TrainerAddress in the query below. Trainer address is mapped as a component of Trainer.
session.QueryOver<Trainer>()
.JoinAlias(x=>x.TrainerAddress.City, ()=> cityAlias, JoinType.LeftOuterJoin)
.OrderBy(x => x.Name).Asc
.SelectList(list => list
.Select(x => x.Id).WithAlias(() => dto.Id)
.Select(x => x.Name).WithAlias(() => dto.Name)
.Select(x => x.TrainerAddress.City.Name).WithAlias(() => dto.City))//issue projecting here
.TransformUsing(Transformers.AliasToBean<TrainerDTO>())
.List<TrainerDTO>();
Is this possible?
You're specifying an alias for TrainerAddress.City but you don't use that alias anywhere?
I'm not 100% sure if NHibernate supports components well in QueryOver, I know there were some issues with it using Criteria, but maybe this will work:
session.QueryOver<Trainer>()
.JoinAlias(x=> x.TrainerAddress.City, ()=> cityAlias, JoinType.LeftOuterJoin)
.OrderBy(x => x.Name).Asc
.SelectList(list => list
.Select(x => x.Id).WithAlias(() => dto.Id)
.Select(x => x.Name).WithAlias(() => dto.Name)
.Select(x => cityAlias.Name).WithAlias(() => dto.City))
.TransformUsing(Transformers.AliasToBean<TrainerDTO>())
.List<TrainerDTO>();

NHibernate fetch with transformation

im trying to query using QueryOver and im trying to do some transformation with fetching.
But im hitting error
HSPTransactionDto hspTransactionDto = null;
var hspTransactionDtoList =
Session.QueryOver<Transaction>()
.Where(x => x.TransactionStatus == TransactionStatus.Draft)
.Fetch(x => x.HealthServiceProvider).Eager
.Fetch(x => x.HealthMaintenanceOrganization).Eager
.SelectList(list => list
.Select(x => x.Id).WithAlias(() => hspTransactionDto.Id)
.Select(x => x.Version).WithAlias(() => hspTransactionDto.Version)
.Select(x => x.HealthServiceProvider.Id).WithAlias(() => hspTransactionDto.HSPId)
.Select(x => x.HealthServiceProvider.Name).WithAlias(() => hspTransactionDto.HSPName)
.Select(x => x.HealthMaintenanceOrganization.Id).WithAlias(() => hspTransactionDto.HMOId)
.Select(x => x.HealthMaintenanceOrganization.Name).WithAlias(() => hspTransactionDto.HMOName)
.Select(x => x.HSPPatientCode).WithAlias(() => hspTransactionDto.HSPPatientCode)
.Select(x => x.PatientFirstName).WithAlias(() => hspTransactionDto.PatientFirstName)
.Select(x => x.PatientMiddleName).WithAlias(() => hspTransactionDto.PatientMiddleName)
.Select(x => x.PatientLastName).WithAlias(() => hspTransactionDto.PatientLastName)
)
.TransformUsing(Transformers.AliasToBean<HSPTransactionDto>()).List<HSPTransactionDto>();
return hspTransactionDtoList;
anyone can help?
actually found the answer just need to put some alias so i ended up with
HSPTransactionDto hspTransactionDto = null;
HealthMaintenanceOrganization hmoAlias = null;
HealthServiceProvider hspAlias = null;
var hspTransactionDtoList =
Session.QueryOver<Transaction>()
.Where(a => a.TransactionStatus == TransactionStatus.Draft)
.Fetch(b => b.HealthServiceProvider).Eager
.Fetch(c => c.HealthMaintenanceOrganization).Eager
.JoinAlias(x=> x.HealthMaintenanceOrganization,() => hmoAlias)
.JoinAlias(x => x.HealthServiceProvider, () => hspAlias)
.SelectList(list => list
.Select(x => x.Id).WithAlias(() => hspTransactionDto.Id)
.Select(x => x.Version).WithAlias(() => hspTransactionDto.Version)
.Select(x => hspAlias.Id).WithAlias(() => hspTransactionDto.HSPId)
.Select(x => hspAlias.Name).WithAlias(() => hspTransactionDto.HSPName)
.Select(x => hmoAlias.Id).WithAlias(() => hspTransactionDto.HMOId)
.Select(x => hmoAlias.Name).WithAlias(() => hspTransactionDto.HMOName)
.Select(x => x.HSPPatientCode).WithAlias(() => hspTransactionDto.HSPPatientCode)
.Select(x => x.PatientFirstName).WithAlias(() => hspTransactionDto.PatientFirstName)
.Select(x => x.PatientMiddleName).WithAlias(() => hspTransactionDto.PatientMiddleName)
.Select(x => x.PatientLastName).WithAlias(() => hspTransactionDto.PatientLastName)
)
.TransformUsing(Transformers.AliasToBean<HSPTransactionDto>()).List<HSPTransactionDto>();
return hspTransactionDtoList;
anyone know a cleaner way?
I think you don't need to use Fetch, because you've used Select for selective properties and it's grouped with DTO:
HSPTransactionDto hspTransactionDto = null;
HealthMaintenanceOrganization hmoAlias = null;
HealthServiceProvider hspAlias = null;
var hspTransactionDtoList =
Session.QueryOver<Transaction>()
.Where(a => a.TransactionStatus == TransactionStatus.Draft)
.JoinAlias(x=> x.HealthMaintenanceOrganization,() => hmoAlias)
.JoinAlias(x => x.HealthServiceProvider, () => hspAlias)
.SelectList(list => list
.Select(x => x.Id).WithAlias(() => hspTransactionDto.Id)
.Select(x => x.Version).WithAlias(() => hspTransactionDto.Version)
.Select(x => hspAlias.Id).WithAlias(() => hspTransactionDto.HSPId)
.Select(x => hspAlias.Name).WithAlias(() => hspTransactionDto.HSPName)
.Select(x => hmoAlias.Id).WithAlias(() => hspTransactionDto.HMOId)
.Select(x => hmoAlias.Name).WithAlias(() => hspTransactionDto.HMOName)
.Select(x => x.HSPPatientCode).WithAlias(() => hspTransactionDto.HSPPatientCode)
.Select(x => x.PatientFirstName).WithAlias(() => hspTransactionDto.PatientFirstName)
.Select(x => x.PatientMiddleName).WithAlias(() => hspTransactionDto.PatientMiddleName)
.Select(x => x.PatientLastName).WithAlias(() => hspTransactionDto.PatientLastName)
)
.TransformUsing(Transformers.AliasToBean<HSPTransactionDto>()).List<HSPTransactionDto>();
return hspTransactionDtoList;