Print Page | Close Window

Create a Custom Property of an Entity whose value is based on a query

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3546
Printed Date: 13-May-2026 at 4:39am


Topic: Create a Custom Property of an Entity whose value is based on a query
Posted By: markfinch
Subject: Create a Custom Property of an Entity whose value is based on a query
Date Posted: 18-Jul-2012 at 9:11am
Hi All,
 
Not sure if I am in the correct forum for this post but here goes...
 
I am wanting to create a property against a "Service" object lets say - "LastService" and would like this property value to be generated by a query.
 
Such that the "LastService" is in itself a "Service" object but is the previous Service for the given Services Customer.
 
The end result I was hoping for is a datagrid of services, each of which has a cell for Last Service and would be the customers last service before the given one in the list.
 
Not really sure where to start, can I create a custom property like this? After all the related Customer to Service for example is a related entity which is queried from the server (when lazy loaded)
 
Thanks in advance
 
Mark



Replies:
Posted By: mgood
Date Posted: 18-Jul-2012 at 10:35am
The preferred approach for things like this is to use a projection query to populate your grid.
 
http://drc.ideablade.com/xwiki/bin/view/Documentation/query-anonymous-projections - http://drc.ideablade.com/xwiki/bin/view/Documentation/query-anonymous-projections
 
So, for example if I understand your scenario correctly a possible query would look like this:
 
var query = from c in Manager.Customers
                   select new {
                        Customer = c,
                        LastService = c.Services.OrderByDescending(x => x.ServiceDate).FirstOrDefault()
                   };
 
This query returns an anonymous type with two properties. The customer entity and the last service entity. As described in the above link you can also project this into your own custom type if you need to pass it around.
 
 



Print Page | Close Window