linq 中的子对象

发布于 2024-09-03 01:32:11 字数 1449 浏览 4 评论 0原文

我在发布之前检查过但找不到解决方案。我是 linq 的新手,理解它正在耗尽我的大脑。我有一个 xml 并想使用 linq 来填充具有子对象的对象。

xml 和我的 linq 如下。我的问题是在这一行

TaskItems = t.Elements("taskdetail").ToList<TaskItem>() //this line doesn't work

如何填充这个子对象?

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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄离 2024-09-10 01:32:11

试试这个:

var tasks = from t in xd.Descendants("taskheader")
            select new Task
            {
                Id = (int)t.Element("id"),
                Name = t.Element("name").Value,
                IsActive = t.Element("isactive").Value == "1",
                TaskItems = t.Elements("taskdetail").Select(e => new TaskItem
                {
                    TaskId = (int)e.Element("taskid"),
                    Name = (string)e.Element("name"),
                    Value = (string)e.Element("value"),
                }).ToList()
            };

Try this:

var tasks = from t in xd.Descendants("taskheader")
            select new Task
            {
                Id = (int)t.Element("id"),
                Name = t.Element("name").Value,
                IsActive = t.Element("isactive").Value == "1",
                TaskItems = t.Elements("taskdetail").Select(e => new TaskItem
                {
                    TaskId = (int)e.Element("taskid"),
                    Name = (string)e.Element("name"),
                    Value = (string)e.Element("value"),
                }).ToList()
            };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文