Print Page | Close Window

Filter an Include()

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=2299
Printed Date: 29-Apr-2025 at 1:13am


Topic: Filter an Include()
Posted By: BillG
Subject: Filter an Include()
Date Posted: 11-Nov-2010 at 1:18pm
I have the following query that fetches a specific employer and all their jobsites. I would also like to fetch all their addresses but only certain ones, for example where the status of the address is 'A'. Can I filter out records on an include()?

public void GetEmployer()

{

Int32 empNo = employer.EmployerNo;

var query = from Employer in Mgr.Employers.Include("EmplSites").Include("Addresses")

where Employer.EmployerNo == empNo

select Employer;

var op = query.ExecuteAsync();

op.Completed += EmployerQueryCompleted;

}

 




Replies:
Posted By: sbelini
Date Posted: 12-Nov-2010 at 12:33pm
Hi BillG,
 
Unfortunatelly you can't filter records on Include's, but you can work around it with:
 
var query = Mgr.Employers.
               .Select (esa => new {
                 employers = esa,
                 emplsites = esa.EmpSites.Where(o => true),
                 addresses = esa.Addresses.Where(o => true)
               });
 
This way your filters will apply. (yes, you need the .Where(o => true)... for now unless you are have some actual statement )
 
The drawback is that your query is now returning an anonymous type. (but you will have only the wanted entities in the cache)
 
Sorry for the "half answer".  I'll work on a more complete response.
 
Best regards,
   Silvio.


Posted By: sbelini
Date Posted: 22-Nov-2010 at 11:27am
Hi BillG,
 
I realized that is best dealing with Anonymous type in a lambda:
 
var op = query.ExecuteAsync(args => {
  foreach (var item in args.Results) {
    _employees.Add(item.employees);
  }
});
 
where _employees would be an ObservableCollection<Employee> for instance.
 
sbelini.



Print Page | Close Window