Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7019427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:10:54+00:00 2026-05-27T23:10:54+00:00

I have an XDocument with a fragment that resembles: <Data> <Row Id=0 ParentId=-1> <!–

  • 0

I have an XDocument with a fragment that resembles:

<Data>
    <Row Id="0" ParentId="-1">
        <!-- stuff -->
    </Row>
    <Row Id="1" ParentId="0">
        <!-- stuff -->
    </Row>
    <Row Id="2" ParentId="0">
        <!-- stuff -->
    </Row>
    <Row Id="3" ParentId="-1">
        <!-- stuff -->
    </Row>
    <Row Id="4" ParentId="3">
        <!-- stuff -->
    </Row>
</Data>

Assume nesting is limited to the example above.
I want to create a data structure – IDictionary<Parent, List<Child>>. I can’t seem to get anything to join correctly. What I have to this point is:

// get lists of data nodes
List<XElement> pRows = xData.Elements(XName.Get("Row"))
                            .Where(e => e.Attribute(XParentId).Value == "-1")
                            .Select(e => e)
                            .ToList();
List<XElement> cRows = xData.Elements(XName.Get("Row"))
                            .Where(e => e.Attribute(XParentId).Value != "-1")
                            .Select(e => e)
                            .ToList();

var dataSets = pRows.GroupJoin(cRows,
                               p => p,
                               c => c.Attribute(XParentId).Value,
                               (p, children) => new {
                                 ParentID = p.Attribute(XId).Value,
                                 Children = children.Select(c => c)
                               });

The compiler is complaining:

The type arguments for method
‘System.Linq.Enumerable.GroupJoin(System.Collections.Generic.IEnumerable,
System.Collections.Generic.IEnumerable,
System.Func, System.Func,
System.Func,TResult>)’
cannot be inferred from the usage. Try specifying the type arguments
explicitly.

I followed a sample from MSDN using the GroupJoin. I didn’t want to use 2 lists – I’d prefer to use a single list of List<XElement> containing all the rows.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:10:55+00:00Added an answer on May 27, 2026 at 11:10 pm

    I think the 2 list approach is cleaner, except I would avoid calling ToList() until the final step or when the list is really needed. You could turn it into one statement, but it would be long and harder to follow.

    To fix your query, you need to change the outer key selector for pRows from p => p to p => p.Attribute(XId).Value, which is the actual ID. Currently it selects the entire element, which cannot be compared to c => c.Attribute(XParentId).Value since they are different types. The updated query would be:

    var dataSets = pRows.GroupJoin(cRows,
                                   p => p.Attribute(XId).Value,
                                   c => c.Attribute(XParentId).Value,
                                   (p, children) => new {
                                     ParentID = p.Attribute(XId).Value,
                                     Children = children.Select(c => c)
                                   });
    

    To avoid using 2 lists you could modify the query above and replace pRows and cRows with their respective queries, but that makes it long and harder on the eyes. In this particular case I prefer expressing the GroupJoin using query syntax since it is much easier to read than the fluent syntax:

    var query = from root in xData.Elements("Row").Where(e => e.Attribute("ParentId").Value == "-1")
                join child in xData.Elements("Row").Where(e => e.Attribute("ParentId").Value != "-1")
                on root.Attribute("Id").Value equals child.Attribute("ParentId").Value
                into rootChild
                select new 
                {
                    ParentId = root.Attribute("Id").Value,
                    Children = rootChild.Select(o => o)
                };
    
    var dict = query.ToDictionary(o => o.ParentId, o => o.Children.ToList());
    

    Should your real problem have more nesting, LINQ probably won’t be the ideal solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a XDocument that looks like this: XDocument outputDocument = new XDocument( new
I have an XDocument that looks similar to <root> <a> <b foo=1 bar=2 />
I have a loaded XDocument that I need to grab all the attributes that
I current have the following XML fragment that I'm parsing to read its values
I have this fragment that demonstrates the problem: <html> <head> <title>height query demo</title> <script
I have a javascript function that builds an URI fragment using some captured parameters,
I am constructing a large HTML document from fragments supplied by users that have
I have an XDocument and have to remove a node and add the very
I have an XDocument object and the ToString() method returns XML without any indentation.
I have an XDocument class with the XML contents already made. I basically want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.