New Posts New Posts RSS Feed: Create a Custom Property of an Entity whose value is based on a query
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

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

 Post Reply Post Reply
Author
markfinch View Drop Down
Newbie
Newbie


Joined: 21-Jan-2012
Location: Sheffield UK
Posts: 5
Post Options Post Options   Quote markfinch Quote  Post ReplyReply Direct Link To This Post Topic: Create a Custom Property of an Entity whose value is based on a query
    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
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 18-Jul-2012 at 10:35am
The preferred approach for things like this is to use a projection query to populate your grid.
 
 
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.
 
 


Edited by mgood - 18-Jul-2012 at 11:35am
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down