Print Page | Close Window

Filtering Navigation Collections

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=2619
Printed Date: 19-Jul-2026 at 11:28am


Topic: Filtering Navigation Collections
Posted By: gshelton
Subject: Filtering Navigation Collections
Date 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.



Replies:
Posted By: DenisK
Date Posted: 18-Apr-2011 at 1:01pm
Hi gshelton;

See if this post helps.

http://www.ideablade.com/forum/forum_posts.asp?TID=2350&KW=active+book&PID=9418&title=soft-deletes-and-linq#9418 - http://www.ideablade.com/forum/forum_posts.asp?TID=2350&KW=active+book&PID=9418&title=soft-deletes-and-linq#9418



Posted By: gshelton
Date 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.


Posted By: DenisK
Date 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.


Posted By: gshelton
Date Posted: 26-Apr-2011 at 11:11am
Great.  Thanks!



Print Page | Close Window