使用LINQ获取特定项目的时间序列

时间:2013-06-20 00:32:31

标签: c# linq

我会尝试总结一下我的问题:

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

我正在尝试将结果输入如下所示的类:

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

基本上:我需要像(productId, Year, Value)

这样的东西
Product1, Dictionary( {2000, 10} {2001, 11},{2002, 13},{2003, 14},{2004, 15}}

Product2, Dictionary( {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, Dictionary( {2000, 10} {2001, 11},{2002, 13},{2003, 14},{2004, 15}}

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

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

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

Product1, Dictionary( {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();
}

1 个答案:

答案 0 :(得分:1)

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

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的截图):

Result