child objects in linq
Posted
by gangt
on Stack Overflow
See other posts from Stack Overflow
or by gangt
Published on 2010-05-28T21:54:39Z
Indexed on
2010/05/28
22:02 UTC
Read the original article
Hit count: 147
linq-to-xml
I checked before I posted but couldn't find a solution. I'm new to linq and it is draining my brain to understand it. I have an xml and want to use linq to fill object that has a child object.
the xml and my linq is below. My issue is on this line
TaskItems = t.Elements("taskdetail").ToList<TaskItem>() //this line doesn't work
how do I fill this child object?
var task1 = from t in xd.Descendants("taskheader")
select new
{
Id = t.Element("id").Value,
Name = t.Element("name").Value,
IsActive = Convert.ToBoolean(Convert.ToInt16(t.Element("isactive").Value))
TaskItems = t.Elements("taskdetail").ToList<TaskItem>()
};
<tasks>
<taskheader>
<id>1</id>
<name>some task</name>
<isactive>1</isactive>
<taskdetail>
<taskid>1</taskid>
<name>action1</name>
<value>some action</value>
</taskdetail>
<taskdetail>
<taskid>1</taskid>
<name>action2</name>
<value>some other action</value>
</taskdetail>
</taskheader>
</tasks>
public class Task
{
public int Id;
public string Name;
public bool IsActive;
public List<TaskItem> TaskItems = new List<TaskItem>();
}
public class TaskItem
{
public int TaskId;
public string Name;
public string Value;
}
© Stack Overflow or respective owner