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 522623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:22:25+00:00 2026-05-13T08:22:25+00:00

I am learning LINQ and this seems like a fairly simple problem. I have

  • 0

I am learning LINQ and this seems like a fairly simple problem. I have a semi-working solution but I am sure it could be cleaned up.

The propertyAuto node represents a car with id = 606.
This node needs to have at least two child propertyValue nodes, one that references a vehicleValuation element with attribute of “Book” and one of “Auto”. (My code doesnt check this yet, they could both be Auto or Book now)

Finally, I need to get the Book value and Auto Value for the same car.

Note that I will never know any IDs or idRef’s beforehand and in the future there will be multiple cars.

Here is my code now (ready to copy into LINQPad!)

var data = XElement.Parse (@"
<MyXML>
  <propertyAuto id='606'>
    <Values>
      <propertyValue idRef='f95d5dce-8152-4e9e-889e-7433d32664d6' />
      <propertyValue idRef='cd1a83a7-dd04-41f9-b31c-5408a38ac777' />
    </Values>
  </propertyAuto>
  <Valuations>
    <vehicleValuation id='cd1a83a7-dd04-41f9-b31c-5408a38ac777'
    valuationType='Auto' estimatedValue='8350.00' />
    <vehicleValuation id='f95d5dce-8152-4e9e-889e-7433d32664d6'
    valuationType='Book' estimatedValue='12475.00' />
  </Valuations>
</MyXML>");

var valuations = from property in data.Descendants("propertyValue")
                 join value in data.Descendants("vehicleValuation")
                 on
                    (string)property.Attribute("idRef")
                 equals
                    (string)value.Attribute("id")
                 where property.Parent.Descendants("propertyValue").Count() > 1
                 && ((string)value.Attribute("valuationType") == "Auto" || (string)value.Attribute("valuationType") == "Book")               
                 select new { Value = value.Attribute("estimatedValue").Value, Type = value.Attribute("valuationType").Value, PropertyID = property.Parent.Parent.Attribute("id").Value };

valuations.Dump();

var values = valuations.GroupBy(x=> x.PropertyID).FirstOrDefault();
string auto = values.Where(x => x.Type == "Auto").Select(x=>x.Value).First().ToString();
string book = values.Where(x => x.Type == "Book").Select(x=>x.Value).First().ToString();

auto.Dump();
book.Dump();

Is this workable or should I move to xpath, etc?

  • 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-13T08:22:25+00:00Added an answer on May 13, 2026 at 8:22 am

    Clearly your solution is workable because it works! I would also take it a little further to get the results as a list of cars with properties that you can easily inspect:

    var carProperties = from propertyAuto in data.Descendants("propertyAuto")
                        select new
                        {
                            Id = propertyAuto.Attributes("id").First().Value,
                            Properties = from property in propertyAuto.Descendants("propertyValue")
                                         join value in data.Descendants("vehicleValuation")
                                         on property.Attribute("idRef").Value
                                         equals value.Attribute("id").Value
                                         select new
                                         {
                                             Value = value.Attribute("estimatedValue").Value,
                                             Type = value.Attribute("valuationType").Value,
                                             PropertyID = property.Parent.Parent.Attribute("id").Value
                                         }
                        };
    
    var cars = from car in carProperties
               select new
               {
                   Id = car.Id,
                   Auto = car.Properties.Where(x => x.Type == "Auto").Select(x => x.Value).First(),
                   Book = car.Properties.Where(x => x.Type == "Book").Select(x => x.Value).First()
               };
    

    Notice that the GroupBy is no longer needed.

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

Sidebar

Related Questions

I'm still learning LINQ. I can do some simple queries, but this one is
I learning LINQ, and i want to use pending request, but i have this
So I've been working on learning LINQ, and I think I'm doing this correctly,
i have just started learning linq because i like the sound of it. and
I'm trying to solve a simple problem using LINQ, which I'm just learning. I
I am learning LINQ and was trying out this example related to select clause.
I am currently learning C# and LINQ. I have lots of questions about them.
I'm learning NHibernate to learn a more robust ORM than linq to sql, but
I'm currently working my way through the learning curve that is LINQ and I
I'm learning the Android api from a book, and it seems like there isn't

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.