如何使用 linq 从分层数据结构投影新类型

发布于 2024-08-23 01:19:47 字数 1111 浏览 1 评论 0原文

我们有一个名为 Group 的类,它包含问题的集合并且它还包含组的集合(即我们有潜在的组嵌套)。

用 XML 表示,这可能看起来像这样:

<group id="Group1">
  <questions>
      <question id="questions11"/>
  </questions>
  <groups>
    <group id="group12">
      <questions>
          <question id="question121"/>
      </questions>
      <groups>
        <group id ="group121">
          <questions>
             <question id="question1211"/>
          </questions>
        </group>
        <group id ="group122">
          <questions>
            <question id="question1221"/>
          </questions>
        </group>        
      </groups>
    </group>
    <group id="group13">
      <questions>
          <question id="question131"/>
      </questions>
    </group>
  </groups>
</group>

我实际上简化了结构,为了简洁起见,我省略了每个组中我想要包含在投影中的属性。

我想做的是塑造一种新类型,其中包括此处的层次结构和一些属性,但排除其他一些属性和问题。

我完全坚持在 linq 中执行此操作。我查看了 SelectMany 但这不是我想要的。

TIA

We've got a Class called Group, it contains a collection of questions and it also contains collection of groups (i.e. we have a potential nesting of groups).

Expressed in XML this might look something like this:

<group id="Group1">
  <questions>
      <question id="questions11"/>
  </questions>
  <groups>
    <group id="group12">
      <questions>
          <question id="question121"/>
      </questions>
      <groups>
        <group id ="group121">
          <questions>
             <question id="question1211"/>
          </questions>
        </group>
        <group id ="group122">
          <questions>
            <question id="question1221"/>
          </questions>
        </group>        
      </groups>
    </group>
    <group id="group13">
      <questions>
          <question id="question131"/>
      </questions>
    </group>
  </groups>
</group>

I've actually simplified the structure, for brevity I've omitted bunch of properties in each group that I'll want to include in the projection.

What I want to do is shape a new type that includes the hierachical structure here and some of the properties but excludes some others and the questions.

I'm utterly stuck on hwo to do this in linq. I had a look at SelectMany but that wasn't what I wanted.

TIA

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

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

发布评论

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

评论(2

古镇旧梦 2024-08-30 01:19:47

您需要创建一个递归函数,将 元素转换为 Group 实例,并为子 调用自身> 元素。

You will need to make a recursive function that turns a <group> element into a Group instance and calls itself for the child <group> elements.

可遇━不可求 2024-08-30 01:19:47

好吧...我不确定您将使用什么对象,但如果它来自来自xml,您可以尝试与此类似的操作:

    public class Group
    {
        public string Id { get; set; }
        IEnumerable<Question> Questions { get; set; }
        IEnumerable<Group> Groups { get; set; }

        public static IEnumerable<Group> Extract(XElement groupElement)
        {
            var groups = new List<Group>();
            var group = new Group{ Id = groupElement.Attribute("id").Value};
            groups.Add(group);

            // Extract questions.
            var questionsSubRoot = groupElement.Element("questions");
            if (questionsSubRoot != null)
                group.Questions = Question.Extract(questionsSubRoot);

            // Extract subgroups.
            var groupsElement = groupElement.Element("groups");
            if (groupsElement != null)
                group.Groups = groupsElement.Elements("group").SelectMany(elem => Group.Extract(elem));

            return groups;
        }
    }

对于您的 Questions 类...

    public class Question
    {
        public string Id { get; set; }

        public static IEnumerable<Question> Extract(XElement questionsElement)
        {
            return questionsElement.Elements().Select(elem => new Question { Id = elem.Attribute("id").Value });
        }
    }

您会称其为...

var xDoc = XDocument.Parse(xml);
var groups = Group.Extract(xDoc.Element("group"));

Well... I'm not sure what objects you would be working with, but if it was coming from xml, you might try something similar to this:

    public class Group
    {
        public string Id { get; set; }
        IEnumerable<Question> Questions { get; set; }
        IEnumerable<Group> Groups { get; set; }

        public static IEnumerable<Group> Extract(XElement groupElement)
        {
            var groups = new List<Group>();
            var group = new Group{ Id = groupElement.Attribute("id").Value};
            groups.Add(group);

            // Extract questions.
            var questionsSubRoot = groupElement.Element("questions");
            if (questionsSubRoot != null)
                group.Questions = Question.Extract(questionsSubRoot);

            // Extract subgroups.
            var groupsElement = groupElement.Element("groups");
            if (groupsElement != null)
                group.Groups = groupsElement.Elements("group").SelectMany(elem => Group.Extract(elem));

            return groups;
        }
    }

and for your Questions class...

    public class Question
    {
        public string Id { get; set; }

        public static IEnumerable<Question> Extract(XElement questionsElement)
        {
            return questionsElement.Elements().Select(elem => new Question { Id = elem.Attribute("id").Value });
        }
    }

You would call it like...

var xDoc = XDocument.Parse(xml);
var groups = Group.Extract(xDoc.Element("group"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文