简体   繁体   English

如何从Linq将子对象映射到父对象

[英]How to map child objects to parent from Linq

I want to to make a list of the object MyProduct the problem is when i map the child objects to the parent object 我想列出对象MyProduct的问题,问题是当我将子对象映射到父对象时

My code looks like this 我的代码看起来像这样

  ShopDatabaseEntities db = new ShopDatabaseEntities();
    var Result1 = (from Products in db.Products
                  join ProductProperties in db.ProductProperties on Products.ProductId equals ProductProperties.ProductId
                  join Properties in db.Properties on ProductProperties.PropertyId equals Properties.PropertyId

                  select new
                  {

                      Products.ProductName,
                      Products.ProductPrice,

                      ProductProperties.PropertyValue,
                      Properties.PropertyName,
                      ProductProperties.PropertyId

                  }).ToList();
    List<MyProduct> lii = new List<MyProduct>();
    foreach (var item in Result1)
    {
        MyProduct pp = new MyProduct();
        pp = (from c in Result1
              select new MyProduct { Name = item.ProductName }).First();

        MyProperty e = new MyProperty();
        e.PropertyName = item.PropertyName;
        e.PropertyValue = item.PropertyValue;

        pp.pros.Add(e);
        lii.Add(pp);
    }

It seems to me that this is more what you want: 在我看来,这更是您想要的:

var query =
    from Products in db.Products
    join ProductProperties in db.ProductProperties on Products.ProductId equals ProductProperties.ProductId
    join Properties in db.Properties on ProductProperties.PropertyId equals Properties.PropertyId
    select new
    {
        ProductProperties.PropertyValue,
        Properties.PropertyName,
        ProductProperties.PropertyId,
        Products.ProductName,
        Products.ProductPrice
    };

List<MyProduct> lii =
    query
        .ToArray()
        .GroupBy(x => new
        {
            x.ProductName,
            x.ProductPrice
        }, x => new
        {
            x.PropertyValue,
            x.PropertyName,
            x.PropertyId
        })
        .Select(x => new MyProduct()
        {
            Name = x.Key.ProductName,
            pros = x
                .Select(y => new MyProperty()
                {
                    PropertyName = y.PropertyName,
                    PropertyValue = y.PropertyValue,
                })
                .ToList()
        })
        .ToList();

BTW, this is the code that you should have included in your question to make answering much easier: 顺便说一句,这是您应该包括在问题中的代码,以使回答更加容易:

public class ShopDatabaseEntities
{
    public List<MyProduct> Products = new List<MyProduct>();
    public List<MyProductProperty> ProductProperties = new List<MyProductProperty>();
    public List<MyProperty> Properties = new List<MyProperty>();
}

public class MyProduct
{
    public int ProductId;
    public string Name;
    public string ProductName;
    public decimal ProductPrice;
    public List<MyProperty> pros = new List<MyProperty>();
}

public class MyProductProperty
{
    public int ProductId;
    public int PropertyId;
    public double PropertyValue;
}

public class MyProperty
{
    public int PropertyId;
    public string PropertyName;
    public double PropertyValue;
}

Type defs help immensely. 键入defs help非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM