[英]Accessing entities with navigation properties using Linq in Web Api and EF
Hi I am trying to write a linq query in web api using etity framework. 嗨,我正在尝试使用etity框架在web api中编写linq查询。 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 MainProj与task_list有1对多的关系我正在使用以下linq查询
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 请告诉我如何在linq查询中访问此行的task_list属性
task_list=c.task_list.(** cant access the properties from task_list here)
As you said there is one to many relation in Mainproj
and task_list
you can do something like following. 正如你所说,
Mainproj
和task_list
有一对多的关系,你可以做类似下面的事情。
Here is a list of Mainproj
这是
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 以下是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 这是快速观看结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.