1

我将尝试总结我的问题:

在我的产品表中,我有不同的产品,在 productPrice 表中,每种产品的价格都会随着时间而变化。

我正在尝试将结果放入如下类:

class ProductDictionary
{
    public int ProductId { get; set; }
    public IDictionary<int,double> YearValues { get; set; }
}

基本上:我需要类似的东西:(productId, Year, Value)

Product1,字典({2000, 10} {2001, 11},{2002, 13},{2003, 14},{2004, 15}}

Product2,字典({2000, 9} {2001, 11},{2002, 18},{2003, 16},{2004, 17} }

我写了以下内容,但它没有给出我想要的

var result = (from x in products
              join y in yearValuesForProduct2 on x.Id equals y.ProductId
              select new ProductDictionary
              {
                 ProductId = x.Id,
                  YearValues = (from z in yearValuesForProduct2
                                where z.ProductId == x.Id
                                select new { Year = z.Year, Value = z.Value })
                                    .ToDictionary(k => k.Year, k => k.Value)
              }).Distinct().ToList();

这返回:

Product1,字典({2000, 10} {2001, 11},{2002, 13},{2003, 14},{2004, 15}}

Product1,字典({2000, 10} {2001, 11},{2002, 13},{2003, 14},{2004, 15}}

Product1,字典({2000, 10} {2001, 11},{2002, 13},{2003, 14},{2004, 15}}

Product1,字典({2000, 10} {2001, 11},{2002, 13},{2003, 14},{2004, 15}}

Product1,字典({2000, 10} {2001, 11},{2002, 13},{2003, 14},{2004, 15}}

换句话说,重复的结果与连接的第二部分一样多。

任何帮助表示赞赏:

所有示例代码

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ProductYearValues
{
    public int ProductId { get; set; }
    public int Year { get; set; }
    public double Value { get; set; }
}

public class ProductDictionary
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public IDictionary<int, double> YearValues { get; set; }
}


static void Main(string[] args)
{
    var newProduct1 = new Product() { Id = 1, Name = "Product 1" };
    var newProduct2 = new Product() { Id = 2, Name = "Product 2" };
    var newProduct3 = new Product() { Id = 3, Name = "Product3" };

    List<Product> products = new List<Product>() { newProduct1 };

    List<ProductYearValues> yearValuesForProduct2 = new List<ProductYearValues>()
    {
        new ProductYearValues() {ProductId =newProduct2.Id, Year = 2005, Value = 20},
        new ProductYearValues() {ProductId =newProduct2.Id, Year = 2006, Value = 22},
        new ProductYearValues() {ProductId =newProduct2.Id, Year = 2007, Value = 22},
        new ProductYearValues() {ProductId =newProduct2.Id, Year = 2009, Value = 23},
        new ProductYearValues() {ProductId =newProduct2.Id, Year = 2010, Value = 24},
        new ProductYearValues() {ProductId =newProduct2.Id, Year = 2011, Value = 25}
    };


    var result = (from x in products
                  join y in yearValuesForProduct2 on x.Id equals y.ProductId
                  select new ProductDictionary
                  {
                      ProductId = x.Id,
                      Name = x.Name,
                      YearValues = (from z in yearValuesForProduct2
                                    where z.ProductId == x.Id
                                    select new { Year = z.Year, Value = z.Value })
                                    .ToDictionary(k => k.Year, k => k.Value)


                      }).Distinct().ToList();
}
4

1 回答 1

1

您的测试代码不会添加newProduct2或添加newProduct3到比较列表中,也不会将年份值放入其他产品中。所以我把测试代码稍微改了一下:

List<Product> products = new List<Product>() { newProduct1, newProduct2, newProduct3 };

List<ProductYearValues> yearValuesForProduct2 = new List<ProductYearValues>()
{
    new ProductYearValues() {ProductId = newProduct1.Id, Year = 2005, Value = 99},
    new ProductYearValues() {ProductId = newProduct1.Id, Year = 2006, Value = 45},

    new ProductYearValues() {ProductId = newProduct2.Id, Year = 2005, Value = 20},
    new ProductYearValues() {ProductId = newProduct2.Id, Year = 2006, Value = 22},
    new ProductYearValues() {ProductId = newProduct2.Id, Year = 2007, Value = 22},
    new ProductYearValues() {ProductId = newProduct2.Id, Year = 2009, Value = 23},
    new ProductYearValues() {ProductId = newProduct2.Id, Year = 2010, Value = 24},
    new ProductYearValues() {ProductId = newProduct2.Id, Year = 2011, Value = 25},

    new ProductYearValues() {ProductId = newProduct3.Id, Year = 2005, Value = 100},
    new ProductYearValues() {ProductId = newProduct3.Id, Year = 2006, Value = 77},
};

其次,这是一个获取结果的 LINQ 查询。我认为关键是按相同的产品对它们进行分组:

var r = products
    .Join(yearValuesForProduct2, p => p.Id, pyv => pyv.ProductId, (p, pyv) => new { Product = p, ProductYearValue = pyv })
    .GroupBy(p => p.Product, p => p.ProductYearValue)
    .Select(p => new ProductDictionary() { 
        ProductId = p.Key.Id,  
        Name = p.Key.Name,
        YearValues = p.ToDictionary(pyv => pyv.Year, pyv => pyv.Value)})
    .ToList();

这个结果是哪些产品(来自 LINQPad 的屏幕截图):

结果

于 2013-06-20T00:55:10.770 回答