2016-07-06 56 views
0

我是編程新手,我正在開發Windows窗體應用程序。我爲我所有的數據庫工作制作了一個轉錄類。我在存儲庫中創建了一個返回IQueryable的函數,它返回一個鍵值對(key = dayOfWeek和value = revenue)。然後當我在窗體中調用這個函數時,因爲我想要將信息打印在標籤上,所以我無法單獨訪問該關鍵字和值。它只給了我整個鍵值對的選項。 這是我在倉庫中類代碼:我無法從函數返回一個函數的值和值,這個函數返回一個我從存儲庫類中獲取的winform中的IQueryable

public class ReportsRepository 
    { 
     FruitStoreDataContext db; 
     public IQueryable RevenuePerDayOfWeek(DateTime startDate, DateTime endDate) 
     { 
      db = new FruitStoreDataContext(); 
     var sumPerday = from s in db.OrderDetails 
         where s.Order.OrderDate >=startDate && s.Order.OrderDate <=endDate 
         select new 
         { 
          day = s.Order.OrderDate.DayOfWeek, 
          revenue = s.Price * s.Quantity 
         }; 

     var totalSumPerday = from f in sumPerday 
          group f.revenue by f.day into g 
          select new 
          { 
           Day= g.Key, 
           Sum = g.Sum() 
          }; 

     return totalSumPerday; 
    } 

private void Report1Form_Load(object sender, EventArgs e) 
     { 
      ReportsRepository report = new ReportsRepository(); 
      var totalSumPerday = report.RevenuePerDayOfWeek(dateToStart, dateToEnd); 
      int[]numOfDays = new int[7]; 


     for (DateTime day = dateToStart; day <= dateToEnd; day = day.AddDays(1)) 
     { 

      dayOfWeek = Convert.ToInt32(day.DayOfWeek); 
      numOfDays[dayOfWeek]++; 

     } 
     Label label; 
     List<Label> labels = new List<Label>(); 
     int t = 0; 

     foreach(var totalSum in totalSumPerday) 
     { 

      if (numOfDays[dayOfWeek] == 0) 
       numOfDays[dayOfWeek] = 1; 
      int y = (38 * t) + 60; 
      label = new Label(); 
      label.Location = new Point(34, y); 
      label.Visible = true; 
      label.Size = new Size(450, 35); 
      label.BackColor = Color.Gray; 
      label.ForeColor = Color.White; 
      label.Font = new Font("Lucida Console", 16); 
      dayOfWeek = Convert.ToInt16(totalSum.Day.Key); 

     //on the line below the word 'Day' and 'Sum' are underlined red...it doesn't give me the option to do that. I can only access the whole thing together(key,value) 
      label.Text = totalSum.Day.ToString() + " : " + (totalSum.Sum/numOfDays[dayOfWeek]).ToString(); 
      labels.Add(label); 
      panel1.Controls.Add(label); 
      t++; 
     } 

回答

0

首先,你可以做一個查詢來實現你的結果。其次,您不需要返回IQueryable,因爲您只需要內存數據(不需要進一步查詢)。所以你可以做的是:

public Dictionary<int, decimal> RevenuePerDayOfWeek(DateTime startDate, DateTime endDate) 
{ 
    db = new FruitStoreDataContext(); 

    var sumPerday = (from s in db.OrderDetails 
        where s.Order.OrderDate >= startDate && s.Order.OrderDate <= endDate 
        group s by s.Order.OrderDate.DayOfWeek into grp 
        select new 
        { 
         Day = grp.Key, 
         Sum = grp.Sum(a => a.Price * a.Quantity) 
        }).ToDictionary(x => x.Day, x => x.Sum); 

    return sumPerday; 
} 

用法在Report1Form_Load

var totalSumPerday = report.RevenuePerDayOfWeek(dateToStart, dateToEnd); 

foreach (var totalSum in totalSumPerday) 
{ 
    DayOfWeek dayOfWeek = totalSum.Key; 
    decimal sum = totalSum.Value; 

    // continue your code 
} 
+0

謝謝十億!現在它完美地工作。 –