Print Page | Close Window

Filter EntityRelationQuery?

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=2328
Printed Date: 21-Aug-2025 at 6:38pm


Topic: Filter EntityRelationQuery?
Posted By: tersiusk
Subject: Filter EntityRelationQuery?
Date Posted: 22-Nov-2010 at 5:16am
Hi

I would like to add filters on my relationship properties. I'm able to intercept the query on the Querying event but it seems I cannot add a filter to the query itself since the Filter() method only works on EntityQuery and not EntityRelationQuery. Is there another way to filter relationship results?

I'm trying to do this since the user logs into a certain application context where he should only be able to see certain records which are linked to that context.





Replies:
Posted By: sbelini
Date Posted: 22-Nov-2010 at 2:55pm
Hi tersiusk,
 
You can trigger the filters by using Select().
In the example below (based on NorthwindIB) both filters for Employees and Orders will fire:
 
var employeesQuery = mgr.Employees
  .Select(eo => new { employees = eo, orders = eo.Orders.Where(o => true
) })
  .ToList()
  .Select(eo => eo.employees)
  .ToList();
Regards,
   sbelini.
 


Posted By: tersiusk
Date Posted: 22-Nov-2010 at 11:46pm
Thanks for the reply. I'm not exactly sure that is what I was referring to.

Here is a quick code sample of what I was trying to do.  This does not seem to be working. I was looking for a way to filter the default generated relation properties but it looks like it's not possible? 

static void em_Querying(object sender, EntityQueryingEventArgs e)
        {
            if (InterceptQueries)
            {
                IEntityQuery newQuery = null;

                if (e.Query.QueryableType == typeof (Application) && e.Query is EntityRelationQuery)
                {
newQuery = currentQuery.Filter((IQueryable<Application> q) => q.
                 Where(a => a.ApplicationID == User.CurrentUser.applicationID));
                }
e.Query = newQuery ?? e.Query;
}
}


Posted By: sbelini
Date Posted: 23-Nov-2010 at 4:52pm
Hi tersiusk,
 
Sorry for the mixup.
 
At the moment it's not possible to use .Filter in this scenario.
 
We have a feature request to implement a method to convert an EntityRelationQuery to an EntityQuery which would make possible the use of Filter(). We plan that feature in one of the near coming releases.
 
Kind regards,
sbelini
 


Posted By: papuashu
Date Posted: 25-Sep-2013 at 5:22am
Has ever been implemented the feature of converting EntityRelationQuery to EntityQuery?
In my case, I need to add Include to an EntityRelationQuery...


Posted By: kimj
Date Posted: 25-Sep-2013 at 6:30pm
Sorry, this feature has not been implemented.   Supporting an Include on the converted EntityRelationQuery would also probably lead to other issues when DevForce merges these results into cache.  For now, for these situations when you need the Include, your best alternative is to avoid lazy loading and explicitly write an EntityQuery.



Print Page | Close Window