Reputation: 3307
Hi I am trying to write a linq query in web api using etity framework. I have two entities
MainProj task_list
p_id t_id
p_name p_id
--------- t_name
navigation properties -------------------
task_list navigation properties
mainProj
MainProj has 1 to many relation with task_list I am using following linq query
var list=( from z in MainProj
select new pList
{
proj_name=z.p_name
proj_id=z.p_id
newtasklist = new List<newtasklist>
{
new newtasklist {
task_list=z.task_list.(** cant access the properties from task_list here)
}}
});
classes are
public class pList
{
public int p_id { get; set; }
public string p_name { get; set; }
public IEnumerable<newtasklist> newtasklist { get; set; }
}
public class newtasklist
{
public int t_id { get; set; }
public string t_name { get; set; }
public int p_id { get; set; }
public pList pList {get; set;}
}
Please let me know how i can access task_list properties at this line in linq query
task_list=c.task_list.(** cant access the properties from task_list here)
Upvotes: 0
Views: 540
Reputation: 2691
As you said there is one to many relation in Mainproj
and task_list
you can do something like following.
Here is a list of Mainproj
List<MainProj> mProjects = new List<MainProj>
{
new MainProj
{
p_id = 1, taskList =new List<task_list> {
new task_list{ p_id=1, t_id = 1, t_name="Dev"},
new task_list{ p_id=1, t_id = 2, t_name="QA"},
}, name="Proj1"
},
new MainProj
{
p_id = 2, taskList =new List<task_list> {
new task_list{ p_id=2, t_id = 1, t_name="RA"},
new task_list{ p_id=2, t_id = 2, t_name="DEV"},
},name="Proj2"
}
};
And following is the LINQ
var lst = (from p in mProjects
select new pList
{
p_id = p.p_id,
p_name = p.name,
newtasklist = p.taskList.Select(x => new newtasklist { p_id = x.p_id, t_id = x.t_id, t_name = x.t_name })
});
here is the Quick watch result
Upvotes: 2