Linq查询优化可获得更快的结果

时间:2012-07-16 09:36:21

标签: c# linq

我有一个带有按钮和datagridview的winform。

场景是:

我有一个winform:

                   using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;

        namespace WindowsFormsApplication1
        {
            public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                    CreateItems();
                }

                private List<Store> stores = new List<Store>();

                public void CreateItems()
                {

                    Store newStore1 = new Store();
                    newStore1.StoreName = "My Store 1";
                    newStore1.City = "My City 1";
                    newStore1.PlannedSales = 10;
                    newStore1.RealizedSales = 5;
                    newStore1.SalesDate = new DateTime(2012, 01, 12, 12, 30, 54, DateTimeKind.Unspecified);

                    Store newStore2 = new Store();
                    newStore2.StoreName = "My Store 2";
                    newStore2.City = "My City 2";
                    newStore2.PlannedSales = 200000;
                    newStore2.RealizedSales = 250000;
                    newStore2.SalesDate = new DateTime(2012, 04, 12, 12, 30, 54, DateTimeKind.Unspecified);

                    Store newStore3 = new Store();
                    newStore3.StoreName = "My Store 3";
                    newStore3.City = "My City 3";
                    newStore3.PlannedSales = 100000;
                    newStore3.RealizedSales = 10000;
                    newStore3.SalesDate = new DateTime(2012, 05, 12, 12, 30, 54, DateTimeKind.Unspecified);

                    Store newStore4 = new Store();
                    newStore4.StoreName = "My Store 1";
                    newStore4.City = "My City 1";
                    newStore4.PlannedSales = 20;
                    newStore4.RealizedSales = 10;
                    newStore4.SalesDate = new DateTime(2012, 02, 12, 12, 30, 54, DateTimeKind.Unspecified);


                    Store newStore5 = new Store();
                    newStore5.StoreName = "My Store 1";
                    newStore5.City = "My City 1";
                    newStore5.PlannedSales = 30;
                    newStore5.RealizedSales = 20;
                    newStore5.SalesDate = new DateTime(2012, 03, 12, 12, 30, 54, DateTimeKind.Unspecified);



                    stores.Add(newStore1);
                    stores.Add(newStore2);
                    stores.Add(newStore3);
                    stores.Add(newStore4);
                    stores.Add(newStore5);
                }

                private void btnQuery_Click(object sender, EventArgs e)
                {
                    var query1 = stores.GroupBy(x => new
                    {
                        x.City,
                        x.StoreName,
                        x.SalesDate.Year,
                        x.SalesDate.Month

                    }).Select(group => new
                    {
                        Planned = group.Sum(x => x.PlannedSales),
                        Realised = group.Sum(x => x.RealizedSales),
                        Count = group.Count(),
                        City = group.Key.City,
                        StoreName = group.Key.StoreName,
                        Year = group.Key.Year,
                        Month = group.Key.Month
                    }).OrderBy(x => x.Year).ThenBy(x => x.Month).Where(x => x.StoreName.Equals("My Store 1"));

                    List<Store> total = new List<Store>();

                    foreach (var value in query1)
                    {
                        Store newStore = new Store()
                        {
                            MonthYear = value.Month.ToString() + " - " + value.Year.ToString(),
                            RealizedSales = value.Realised,
                            PlannedSales = value.Planned

                        };
                        total.Add(newStore);
                    };


                    var query2 = total.Select((s, i) => new
                    {
                        MonthYear = s.MonthYear,
                        RealizedSales = s.RealizedSales + total.Take(i).Sum(sa => sa.RealizedSales),
                        PlannedSales = s.PlannedSales + total.Take(i).Sum(sa => sa.PlannedSales)
                    });

                    List<Store> totalFinal = new List<Store>();


                    foreach (var value in query2)
                    {
                        Store newStore = new Store()
                        {
                            MonthYear = value.MonthYear,
                            RealizedSales = value.RealizedSales,
                            PlannedSales = value.PlannedSales
                        };
                        totalFinal.Add(newStore);
                    };


                    dataGridView1.DataSource = totalFinal;

                }
            }

            public class Store
            {
                public string StoreName { get; set; }
                public string City { get; set; }

                public int PlannedSales { get; set; }
                public int RealizedSales { get; set; }

                public DateTime SalesDate { get; set; }

                public string MonthYear { get; set; }

            }
        }

实例化表单时,将调用CreateItems方法。此方法将使用一些演示商店数据填充列表

当按下表单中的按钮时,将以Linq查询列表商店,其方式将返回所有 PlannedSales RealizedSales 列表商店 MonthYear 商店名称 =我的商店1

查询结果的示例如下:http://i49.tinypic.com/vz1c6.jpg

知道如何优化此查询以使其更简单但获得相同的结果吗?

基本上我只需要为特定商店名称每月返回所有计划和实现的销售额!

谢谢!

2 个答案:

答案 0 :(得分:0)

这应该像以下一样简单:

var totalPlanned = 0;
var totalRealized = 0;
var result = stores.Where(s => s.StoreName.Equals("My Store 1"))
                   .Select(s => {
                        totalPlanned += s.PlannedSales;
                        totalRealized += s.RealizedSales;
                        return new Store(){
                           MonthYear = s.SalesDate.ToString("MM - yyyy"),
                           PlannedSales = totalPlanned,
                           RealizedSales = totalRealized

                        };
                    });

实例显示此新代码与原始代码一起显示相同结果:http://rextester.com/PAF27531

答案 1 :(得分:-1)

from s in stores
where s.StoreName == "My Store 1"
group s by new { s.StoreName, s.SalesDate.Year, s.SalesDate.Month } into g
select new
{
g.Key.StoreName,
MonthYear = g.Key.Year.ToString() + " - " + g.Key.Month.ToString(),
Planned = g.Sum(st => st.PlannedSales),
Realized = g.Sum(st => st.RealizedSales)
}