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++;
}
谢谢十亿!现在它完美地工作。 –