New Posts New Posts RSS Feed: Filter an Include()
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Filter an Include()

 Post Reply Post Reply
Author
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Topic: Filter an Include()
    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;

}

 

Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down