Print Page | Close Window

object custom property

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1830
Printed Date: 16-Apr-2024 at 3:36am


Topic: object custom property
Posted By: bigfish
Subject: object custom property
Date Posted: 19-May-2010 at 1:27pm
Hi guys,
 
I need to sum related objects values to a custom property of parent object to use as a display property in a datagrid source.
 
The equivalent SQL query would look like:
 
SELECT     RR.RentReviewID,
                 RR.ReviewDate,
                 SUM(RRI.IncomeValue) AS TotalIncome
FROM         RentReview RR JOIN
                  RentReviewIncome RRI ON RR.RentReviewId = RRI.RentReviewId
GROUP BY  RR.RentReviewId,
                  RR.ReviewDate
RentReview object has RentReviewIncome as associated object.
 
The query I currently execute is:-
 
var query = Manager.RentReviews
                .Where(a => a.Tenancy.tenancyID == tenancyID)
                .OrderByDescending(a => a.effectiveFromDate)
                .Include("Schedule")
                .Include("RentReviewIncomes");
 
Query(query, callback);
 
This returns associated RentReviewIncome objects, but where to from there?
 
I was hoping something like this custom property on the RentReview object might do the trick, but no.
public decimal TotalIncome { get { return (decimal)RentReviewIncomes.Sum(incomeValue); } }
 



Replies:
Posted By: bigfish
Date Posted: 21-May-2010 at 4:04pm
OK I got it;
 
The partial class RentReview generated by DevForce allowed me to include a custom propertry (TotalIncome) and business rule as per below;
 
public decimal TotalIncome { get { return getIncomes(); }}
 
private decimal getIncomes()
{
   decimal totalIncome = 0;
   foreach(RentReviewIncome income in RentReviewIncomes)
   {
      totalIncome = totalIncome + (decimal)income.incomeValue;
   }
   return totalIncome;
}
 
As its in the partial class, DevForce won't overwrite the rule on each subsequent regeneration.
RentReviewIncomes is the collection of associated entities of the main RentReview object.
 
Very cool for me, maybe very obvious for hardened developers...
 
Rgds
BigFish
 


Posted By: IdeaBlade
Date Posted: 21-May-2010 at 4:32pm
Sorry not to get back to you quicker, but do be aware that the solution you've shown above depends absolutely on the required RentReviewIncomes being in cache at the time the TotalIncome property is accessed. In the asynchronous environment of Silverlight, if they're not there (already), the calculation will simply yield 0.



Posted By: bigfish
Date Posted: 21-May-2010 at 5:27pm
Thanks, good point.  Will keep that in mind while working with the app as a user does to determine if we hit that issue....  much appreciated.



Print Page | Close Window