0

ボタンとデータグリッドビューを備えた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 が呼び出されます。このメソッドは、ListにいくつかのデモStoreデータを取り込みます。

フォームのボタンが押されると、Linq を使用してリスト ストアが照会され、リスト ストアからMonthYearごとにすべてのPlannedSalesRealizedSalesが返されます。 WHERE the StoreName = My Store 1

クエリ結果の例は次のとおりです: http://i49.tinypic.com/vz1c6.jpg

このクエリを最適化してよりシンプルにし、同じ結果を得る方法はありますか?

基本的に、特定の店舗名のみについて、月ごとの年間売上高と計画売上高をすべて返す必要があります。

ありがとう!

4

2 に答える 2

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

于 2012-07-16T10:06:16.083 に答える
-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)
}
于 2012-07-16T10:06:43.727 に答える