开发者

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

开发者 https://www.devze.com 2023-03-12 11:30 出处:网络
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.

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>
    <localizati开发者_StackOverflow中文版on>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>


You should be going through the Product elements, not descendant items. Then it is easier to collect the items per 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();
0

精彩评论

暂无评论...
验证码 换一张
取 消