c# - (N)Hibernate Criteria: summing multiple distinct columns -


none of worded questions on seem match, , googling pretty points this, let's try this:

i have journalentry entity class looks this:

public partial class journalentry {     public virtual guid journalentryid { get; set; }      public virtual account account { get; set; }      public virtual decimal debitamount { get; set; }      public virtual decimal creditamount { get; set; }      [notnull]     public virtual datetime effectivedate { get; set; }      [notnull]     public virtual datetime postingdate { get; set; }      public virtual userprofile postedby { get; set; }      [fulltextindexed]     public virtual string notes { get; set; }      public virtual amortization amortization { get; set; }      public virtual expensecategories expensecategory { get; set; }      [index]     public virtual bool isclosed { get; set; } } 

i have simple class holding transaction summaries so:

public class journalentrysummary {     public decimal credits { get; set; }     public decimal debits { get; set; } } 

what write criteria query return sums of both credits property , debits property. iow, vaguely shaped sql query:

select     sum(creditamount) credits,     sum(debitamount) debits      journalentries     ... 

... , have populate journalentrysummary object. i've seen lots of examples of how 1 column, , examples of adding 2 columns together, no examples of collecting 2 distinct summaries , dumping them non-domain object.

is possible? how it?

take @ criteria queries documentation http://nhibernate.info/doc/nh/en/index.html#querycriteria-projection, there examples answer question.

for example should try this

// using nhibernate.criterion; // using nhibernate.transform;  session.createcriteria<journalentry>()    .setprojection(        projections.sum<journalentry>(x => x.debitamount).as("debits"),        projections.sum<journalentry>(x => x.creditamount).as("credits"),        // can use other aggregates        // projections.rowcount(),        // projections.max<journalentry>(x => x.effectivedate)     )     .setresulttransformer(transformers.aliastobean<journalentrysummary>())     .uniqueresult<journalentrysummary>(); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -