New Posts New Posts RSS Feed: Filtering Navigation Collections
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Filtering Navigation Collections

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

Joined: 17-Sep-2010
Location: Nashville, TN
Posts: 4
Post Options Post Options   Quote gshelton Quote  Post ReplyReply Direct Link To This Post Topic: Filtering Navigation Collections
    Posted: 13-Apr-2011 at 8:12pm
If I have a Customer entity with an Orders navigation collection property, is there a way to extend the Customer entity partial class to expose an ActiveOrders collection property?
 
something like
 
this.Orders.Where(o => o.Active == true)
 
 
Honestly I'm lost as to the return type for ActiveOrders.  Would it be IQueryable<Order>, EntityQuery<Order> or RelatedEntityList<Order>? 
 
Thanks.
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: 18-Apr-2011 at 1:01pm
Back to Top
gshelton View Drop Down
Newbie
Newbie
Avatar

Joined: 17-Sep-2010
Location: Nashville, TN
Posts: 4
Post Options Post Options   Quote gshelton Quote  Post ReplyReply Direct Link To This Post Posted: 21-Apr-2011 at 4:59pm
Ok, so the link above answered my question and while cool, it doesn't allow me to use an Include statement for the filtered collection.    So I found the DRC topic "Include and fitler related entities" that offers a suggestion for filtering a navigation collection in a single query to the database. 
 
I adapted that  example to my model and produced the following:
 
EXAMPLE 1
//Return Standard Work Type and first 10 WorkTypes
var myquery = _manager.StandardWorkTypes.Where(e => e.StandardWorkTypeID == standardWorkTypeID)               .Select(swt => new { StandardWorkType = swt, WorkTypes =  swt.WorkTypes.Take(10)});
This works, but I'd like to be able to return a related entity from WorkTypes. If I'm not worried about filtering the WorkTypes
collection I'd write the query as follows:
EXAMPLE 2
//Return Standard Work Type and all Work Types and their associated HealthcareOrganization
var
 query = from entity in _manager.StandardWorkTypes             where entity.StandardWorkTypeID == standardWorkTypeID             select entity;
query = query.Include("WorkTypes.HealthcareOrganization");
 
QUESTION 1 - Is there a way to query StandardWorkType, take/filter WorkTypes AND include HealthcareOrganization
using the syntax from EXAMPLE 1?
 
 
Before discovering the topic above, I had been issuing two queries, 1 for the StandardWorkType entity and 1 for WorkTypes
entity. This worked, but my ViewModel only exposed the StandardWorkType entity and the grid I'm using was
bound to {Binding StandardWorkType.WorkTypes}. So even though I had loaded the data I wanted in a
separate query, DevForce went back to the database because it was the first time I had accessed the WorkTypes
navigation property for my entity. I then discovered the DRC topic "Navigation properties and data retrieval" and set a
new EntityReferenceStrategy using
 
DomainModel.StandardWorkType.PropertyMetadata.WorkTypes.ReferenceStrategy = new EntityReferenceStrategy(EntityReferenceLoadStrategy.DoNotLoad, MergeStrategy.PreserveChanges);
This works as expected and is just beautiful. I also get the same results if I use
 
DomainModel.StandardWorkType.PropertyMetadata.WorkTypes.GetEntityReference(standardWorkType).IsLoaded = true;
 
QUESTION 2 - Do you see any red flags here? If I want to set the EntityReferenceStrategy application wide using
the first method, should I do that on application startup? The ERS is app wide across all Entity Mangers, right?
 
 
Thanks for your help.


Edited by gshelton - 21-Apr-2011 at 5:01pm
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: 25-Apr-2011 at 3:56pm
Hi gshelton;

Answer to Question 1:

Yes. You just add another anonymous property as follows.


var customerQuery = mgr.Customers
        .Where(c => c.Country == "USA")
        .Select(cust => new {
          aCustomer = cust,
          Orders = cust.Orders.Take(10),
          OrderDetails = cust.Orders.Take(10).SelectMany(o => o.OrderDetails)
        });


Answer to Question 2:

I think your use case seems reasonable. Yes, you should do this on application startup if you want every access of StandardWorkType.WorkTypes to not go to the database during the application life cycle. The EntityReferenceStrategy property is a global setting across all entities and it is not dependent on any EntityManager.
Back to Top
gshelton View Drop Down
Newbie
Newbie
Avatar

Joined: 17-Sep-2010
Location: Nashville, TN
Posts: 4
Post Options Post Options   Quote gshelton Quote  Post ReplyReply Direct Link To This Post Posted: 26-Apr-2011 at 11:11am
Great.  Thanks!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down