New Posts New Posts RSS Feed: Using Entity Linq with Custom Properties
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Using Entity Linq with Custom Properties

 Post Reply Post Reply
Author
mseeli View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Sep-2010
Location: Switzerland
Posts: 31
Post Options Post Options   Quote mseeli Quote  Post ReplyReply Direct Link To This Post Topic: Using Entity Linq with Custom Properties
    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. 
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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));


Hope this helps.
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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.

 

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down