Print Page | Close Window

Using Entity Linq with Custom Properties

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=2457
Printed Date: 05-Feb-2026 at 6:40pm


Topic: Using Entity Linq with Custom Properties
Posted By: mseeli
Subject: Using Entity Linq with Custom Properties
Date Posted: 21-Jan-2011 at 6:07am
I have a large number of orders.
Each order is of a type.
The types are in a hierarchical tree: there is one master-type and then many branches and layers down of subtypes and sub-types of those and so on.
I want to be able to query the orders by selecting one type and getting back all the orders of this type plus all the orders that are of a subtype of this type (all the way down).

I created a property on the type called IsOfType(Type) in which I check if a type belongs to the branch of certain type.

It works fine. But I have to first get all the orders down to my client before I can use this property in a linq where statement


var ordersOfCertainType = orders.Where(x=>x.Type.IsOfType( typeToBeSelected));

What I would like to do is to use my property in a Entity query i.e.:

manager.GetQuery<Order>.Where(.......) this does not work. Is there a way to get it to work with custom properties? 

and if not how would I go about creating a function that runs on the server that does the selection before it sends the resulting orders over the wire.

thank you. 



Replies:
Posted By: DenisK
Date Posted: 21-Jan-2011 at 5:15pm
Hi mseeli;

You would actually need to use OfType<> with your query. Constructing a dynamic OfType Linq query is currently not possible. 

To create a similar function that runs on the server, you would need to create a server method and call that server method using EntityManager.InvokeServerMethod or EntityManager.InvokeServerMethodAsync. You can pass in the typeToBeSelected argument to that server method and run the same logic as 

var ordersOfCertainType = orders.Where(x=>x.Type.IsOfType( typeToBeSelected));

Please see  http://drc.ideablade.com/xwiki/bin/view/Documentation/QueryRSMC - http://drc.ideablade.com/xwiki/bin/view/Documentation/QueryRSMC  for some code samples.

Hope this helps.


Posted By: DenisK
Date Posted: 28-Jan-2011 at 12:19pm

Hi mseeli; 

I just discovered that the following can be done to create a dynamic OfType query, not quite a Linq one though. Hope it can be useful to you in some ways.

Employee - Master type

CurrentEmployee - Sub type of Employee

var typeToBeSelected = typeof(CurrentEmployee);

var qBaseWeak = (IQueryable)TypeFns.ConstructGenericInstance(typeof(EntityQuery<>), entityType);

var rBaseWeak = mgr.ExecuteQuery((IEntityQuery)qBaseWeak);

typeToBeSelected could be a sub type and when I tested it, it was able to retrieve the sub type without having to retrieve all the master type first into the client.

 




Print Page | Close Window