Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I dynamically add Include to ObjectSet<Entity> using C#?

I want to add Includes dynamically from input params[]. How can I do this?

This is my code

IQueryable<Work> query = this.ObjectContext.Works
    .Include("EmployeeSender.Person")
    .Include("EmployeeReceiver.Person")
    .Include("WorkCode")
    .Include("WorkFlowStep.WorkFlowFormState")
    .Include("WorkFlow")
    .Include("WorkRoot.EmployeeSender.Person")
    .Include("WorkParent");
like image 379
Masoomian Avatar asked Apr 17 '13 07:04

Masoomian


1 Answers

In a loop, for example:

IQueryable<Work> query = null;  

query = this.ObjectContext.Works;
foreach (var param in params)
{
    query = query.Include(param);
}
var result = query.ToList();

As Christian Dietz mentioned, you can then put this in an extension method so that it becomes reusable.

like image 118
L-Four Avatar answered Sep 28 '22 21:09

L-Four