2

I have a nested document structure like:

[
  {
    "id": "parent1",
    "children": [
      {
        "id": "child1",
        "foo": "bar"
      },
      {
        "id": "child2",
        "foo": "bar"
      },
      {
        "id": "child3",
        "foo": "bar"
      }
    ]
  },
  {
    "id": "parent2",
    "children": [
      {
        "id": "child4",
        "foo": "bar"
      },
      {
        "id": "child5",
        "foo": "bar"
      }
    ]
  }
]

I am able to write the following query in SQL syntax:

SELECT child, parent.id
FROM parent
JOIN child in parent.children

This gets me the following result:

[
  {
    "child": {
      "id": "child1",
      "foo": "bar"
    },
    "id": "parent1"
  },
 ...
]

I wrote a similar query in LINQ using SelectMany clause as follows, but it throws an error stating SelectMany can have only 2 arguments.

collection.SelectMany(
    parent => parent.children,
    (parent, child) => new { child, parent.id });
1
  • Your query looks quite OK to me. Is it the O-R mapper that doesn't understand this SelectMany overload? Exact error message? In case of yes, the answer of dasblinkenlight could solve it. Commented Aug 7, 2017 at 14:40

1 Answer 1

4

You need to "push" the second lambda inside the first lambda with a nested Select, like this:

collection.SelectMany(
    parent => parent.children.Select(child => new {
        Child = child
    ,   ParentId = parent.id
    })
);
Sign up to request clarification or add additional context in comments.

1 Comment

This is one of those situations where you google something for a long period of time and then stumble upon the answer almost randomly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.