Linq - How to select a new object to contain a list of other objects?

I need help creating a linq query that selects a list of Product objects. Each product object contains a ProductItem list. The part that I don’t know how to do is create a Product.ProductItems list. Can someone give me a hand. Here is the Product, ProductItem, and an example of the xml structure I'm playing with.

Here is an example of the direction I was going with this:

XDocument xDocument = XDocument.Load("../Content/index.xml");
            return xDocument.Descendants("item")
                .Select(arg =>
                        new Product
                        {
                            Name = arg.Parent.Attribute("name").Value,
                            ProductItems = new ProductItem{//set properties for PI} // This is where Im stuck.


                        })
                .ToList();
        }

I'm trying to sharpen my linq / lambda skills, so if you could give me an example that uses lambda syntax, I would appreciate it!

Thanks a ton.

public class Product
{
    public string Name { get; set; }
    public IList<ProductItem> ProductItems { get; set; }

}

public class ProductItem
{
    public string Hwid { get; set; }
    public string Href { get; set; }
    public string Localization { get; set; }
    public DateTime BuildDateTime { get; set; }
    public string IcpBuildVersion { get; set; }
}

}

<products>
<product name="Product1">
  <item hwid="abk9184">
    <href>Product1/abk9184_en-us/abk9184.html</href>
    <localization>en-us</localization>
    <build.start>2011-06-08 22:02 PM</build.start>
    <build.icp>9.0.192.32</build.icp>
  </item>
  <item hwid="abk9185">
    <href>LearningModules/abk9185_en-us/abk9185.html</href>
    <localization>en-us</localization>
    <build.start>2011-06-08 22:03 PM</build.start>
    <build.icp>9.0.192.32</build.icp>
  </item>
</product>
<product name="Product2">
  <item hwid="aa6410">
    <href>Product2/aa6410_en-us/aa6410.html</href>
    <localization>en-us</localization>
    <build.start>2011-06-08 22:04 PM</build.start>
    <build.icp>9.0.192.32</build.icp>
  </item>
  <item hwid="tu6488">
    <href>Product2/tu6488_en-us/tu6488.html</href>
    <localization>en-us</localization>
    <build.start>2011-06-08 22:04 PM</build.start>
    <build.icp>9.0.192.32</build.icp>
  </item>
+3
source share
1 answer

Product, . , Product. .

var doc = XDocument.Load("../Content/index.xml");
var products = doc.Elements("product")
    .Select(p =>
        new Product
        {
            Name = (string)p.Attribute("name"),
            ProductItems = p.Elements("item")
                .Select(i =>
                    new ProductItem
                    {
                        //set properties for PI
                        Hwid = (string)i.Attribute("hwid"),
                        Href = (string)i.Element("href"),
                        Localization = (string)i.Element("localization"),
                        // etc.
                    })
                .ToList()
        })
    .ToList();
+12

All Articles