如何在 VB.Net 中使用 LINQ 比较和求和多个周期的值

发布于 2024-12-06 03:54:16 字数 677 浏览 0 评论 0原文

我有以下示例数据表:

Value1 Value2 Customer Product Date

100     50   1000     100     1.8.2010

50      20   1000     101     5.1.2010

200     60   1000     100     6.2.2011

180     100  1001     100     7.3.2010

500     700  1000     100     1.1.2010

300     300  1001     100     4.4.2011

250     600  1000     100     3.3.2011

现在用户应该能够比较多个时期。在此示例中,用户选择了两个时间段:2010 年 1 月 1 日 - 2010 年 12 月 31 日和 2011 年 1 月 1 日 - 2011 年 12 月 31 日。该示例的结果应该是:

Customer Product SumValue1Period1 SumValue2Period1 SumValue1Period2 SumValue2Period2

1000 100 600 750 450 660

1000 101 50 20 0 0

1001 100 300 100 300 300

我该怎么做?

I have the following example datatable:

Value1 Value2 Customer Product Date

100     50   1000     100     1.8.2010

50      20   1000     101     5.1.2010

200     60   1000     100     6.2.2011

180     100  1001     100     7.3.2010

500     700  1000     100     1.1.2010

300     300  1001     100     4.4.2011

250     600  1000     100     3.3.2011

And now the user should be able to compare multiple periods. In this example the user chose two periods: 1.1.2010 - 31.12.2010 and 1.1.2011 - 31.12.2011. The result of the example should be:

Customer Product SumValue1Period1 SumValue2Period1 SumValue1Period2 SumValue2Period2

1000 100 600 750 450 660

1000 101 50 20 0 0

1001 100 300 100 300 300

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日 2024-12-13 03:54:16

由于您已知列数,因此您可以按客户和产品对数据进行分组,然后从分组中获取条件总和,这将生成结果查询的不同列。

请看一下下面的 LinqPad 程序。抱歉,我不熟悉 VB.Net,所以我用 C# 对其进行了编码,但您会明白的:

void Main()
{
    var Period1Start = new DateTime(2010,1,1);
    var Period1End = new DateTime(2010,12,31);
    var Period2Start = new DateTime(2011,1,1);
    var Period2End = new DateTime(2011,12,31);
    List<Item> lst = new List<Item>
    {
      new Item{ Value1 = 100, Value2 = 50, Customer = 1000, Product = 100 , Date = new DateTime(2010,8,1)},
      new Item{ Value1 = 50, Value2 = 20, Customer = 1000, Product = 101 , Date = new DateTime(2010,5,1)},
      new Item{ Value1 = 200, Value2 = 60, Customer = 1000, Product = 100 , Date = new DateTime(2011,2,6)},
      new Item{ Value1 = 180, Value2 = 100, Customer = 1001, Product = 100 , Date = new DateTime(2010,7,3)},    
      new Item{ Value1 = 500, Value2 = 700, Customer = 1000, Product = 100 , Date = new DateTime(2010,1,1)},
      new Item{ Value1 = 300, Value2 = 300, Customer = 1001, Product = 100 , Date = new DateTime(2011,4,4)},
      new Item{ Value1 = 250, Value2 = 600, Customer = 1000, Product = 100 , Date = new DateTime(2011,3,3)} 

    };

    var grp = lst.GroupBy(x=>new{x.Customer, x.Product}).
    Select(y=> new
    {
        Customer = y.Key.Customer,
        Product = y.Key.Product,
        SumValue1Period1 = y.Where(x=>x.Date >= Period1Start && x.Date<= Period1End).Sum(p=>p.Value1),
        SumValue2Period1 = y.Where(x=>x.Date >= Period1Start && x.Date<= Period1End).Sum(p=>p.Value2),
        SumValue1Period2 = y.Where(x=>x.Date >= Period2Start && x.Date<= Period2End).Sum(p=>p.Value1),
        SumValue2Period2 = y.Where(x=>x.Date >= Period2Start && x.Date<= Period2End).Sum(p=>p.Value2)

    });
    Console.WriteLine(grp);
}

// Define other methods and classes here
public class Item
{
    public int Value1{get;set;}
    public int Value2{get;set;}
    public int Customer{get;set;}
    public int Product{get;set;}
    public DateTime Date{get;set;}
}

Since you have known number of columns, you can group data by Customer and products and then take conditional sum from grouping and it will make different columns of the resultant query.

Please have a look at following LinqPad program. Sorry, I'm not familiar with VB.Net so I have coded it in C#, but you'll get the fair idea:

void Main()
{
    var Period1Start = new DateTime(2010,1,1);
    var Period1End = new DateTime(2010,12,31);
    var Period2Start = new DateTime(2011,1,1);
    var Period2End = new DateTime(2011,12,31);
    List<Item> lst = new List<Item>
    {
      new Item{ Value1 = 100, Value2 = 50, Customer = 1000, Product = 100 , Date = new DateTime(2010,8,1)},
      new Item{ Value1 = 50, Value2 = 20, Customer = 1000, Product = 101 , Date = new DateTime(2010,5,1)},
      new Item{ Value1 = 200, Value2 = 60, Customer = 1000, Product = 100 , Date = new DateTime(2011,2,6)},
      new Item{ Value1 = 180, Value2 = 100, Customer = 1001, Product = 100 , Date = new DateTime(2010,7,3)},    
      new Item{ Value1 = 500, Value2 = 700, Customer = 1000, Product = 100 , Date = new DateTime(2010,1,1)},
      new Item{ Value1 = 300, Value2 = 300, Customer = 1001, Product = 100 , Date = new DateTime(2011,4,4)},
      new Item{ Value1 = 250, Value2 = 600, Customer = 1000, Product = 100 , Date = new DateTime(2011,3,3)} 

    };

    var grp = lst.GroupBy(x=>new{x.Customer, x.Product}).
    Select(y=> new
    {
        Customer = y.Key.Customer,
        Product = y.Key.Product,
        SumValue1Period1 = y.Where(x=>x.Date >= Period1Start && x.Date<= Period1End).Sum(p=>p.Value1),
        SumValue2Period1 = y.Where(x=>x.Date >= Period1Start && x.Date<= Period1End).Sum(p=>p.Value2),
        SumValue1Period2 = y.Where(x=>x.Date >= Period2Start && x.Date<= Period2End).Sum(p=>p.Value1),
        SumValue2Period2 = y.Where(x=>x.Date >= Period2Start && x.Date<= Period2End).Sum(p=>p.Value2)

    });
    Console.WriteLine(grp);
}

// Define other methods and classes here
public class Item
{
    public int Value1{get;set;}
    public int Value2{get;set;}
    public int Customer{get;set;}
    public int Product{get;set;}
    public DateTime Date{get;set;}
}
没︽人懂的悲伤 2024-12-13 03:54:16

看看
http://msdn.microsoft.com/en-us/vbasic/bb737908

具体来说,是“GroupBy - 嵌套”示例。它展示了使用 LINQ 按“客户订单,首先按年,然后按月”进行分组。您的情况应该更直接,因为这只是日期范围。

Take a look at
http://msdn.microsoft.com/en-us/vbasic/bb737908

Specifically, the 'GroupBy - Nested' example. It shows using LINQ to group by 'customer's orders, first by year, and then by month.' You situation should be more straight forward since it's just the date range.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文