C# – Linq – How Do I select a new object to contain a list of other objects

c++lambdalinq

I need some help making a linq query that will select a list of Product objects. Each product object contains a list of ProductItem. The part that I am not sure how to do is how to create the Product.ProductItems list. Can someone give me a hand. Here is the Product, ProductItem, and an example of the xml structure Im 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 am trying to sharpen my linq/lambda skills so if you could give me and example that uses the 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>

Best Solution

You should be going through the Product descendants, not items. That way you are reading off of the Product elements. Then it is easier to get the associated items.

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();