我想在下拉列表中显示所选月份,该下拉列表将在 GRIDVIEW 中显示附有各自orderid,finalunitprice,rawcost的食品标题,以及根据累计计算总销售额最终价格和利润
但是我在数据库中遇到 Datetime 问题,因为我不确定如何将DateTime转换为仅获取月份值。
这是我的代码。
的.aspx
<asp:DropDownList ID="DropDownListMonth" runat="server" Width="200px">
<asp:ListItem Value="1">January</asp:ListItem>
<asp:ListItem Value="2">February</asp:ListItem>
<asp:ListItem Value="3">March</asp:ListItem>
<asp:ListItem Value="4">April</asp:ListItem>
<asp:ListItem Value="5">May</asp:ListItem>
<asp:ListItem Value="6">June</asp:ListItem>
<asp:ListItem Value="7">July</asp:ListItem>
<asp:ListItem Value="8">August</asp:ListItem>
<asp:ListItem Value="9">September</asp:ListItem>
<asp:ListItem Value="10">October</asp:ListItem>
<asp:ListItem Value="11">November</asp:ListItem>
<asp:ListItem Value="12">December</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Button ID="BtnSearch" runat="server" Text="Search" OnClick="BtnSearch_Click" /></td>
</tr>
</table>
<hr />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblShowMonth" runat="server" Text='<%# Eval("month") %>'></asp:Label>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
EmptyDataText="No records found">
<Columns>
<asp:TemplateField HeaderStyle-Font-Size="Large" HeaderText="Order Id">
<ItemTemplate>
<asp:Label ID="lblOrderId" runat="server" Text='<%# Eval("orderid") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Height="50px" Width="175px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Font-Size="Large" HeaderText="Food Title">
<ItemTemplate>
<asp:Label ID="lblFoodTitle" runat="server" Text='<%# Eval("foodtitle") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Font-Size="Large" HeaderText="Final Unit Price">
<ItemTemplate>
<asp:Label ID="lblFinalUnitPrice" runat="server" Text='<%# Eval("finalunitprice") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Font-Size="Large" HeaderText="Raw Cost">
<ItemTemplate>
<asp:Label ID="lblRawCost" runat="server" Text='<%# Eval("rawcost") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<table>
<tr>
<td>
<asp:Label ID="lblSales" runat="server" Text="Total Sales:"></asp:Label>
</td>
<td>
<asp:Label ID="lblTotalSales" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblProfit" runat="server" Text="Total Profit:"></asp:Label>
</td>
<td>
<asp:Label ID="lblTotalProfit" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
aspx.cs
protected void BtnSearch_Click(object sender, EventArgs e)
{
int month = Convert.ToInt32(DropDownListMonth.SelectedItem.ToString());
double rawCost = 0.0;
double grossProfit = 0.0;
double profit = 0.0;
if (!this.IsPostBack)
{
var db = new FoodOrderingDBContext();
GridView1.DataSource = (from OrderTable in db.OrderTables
join OrderDetail in db.OrderDetails
on OrderTable.OrderID equals OrderDetail.OrderID
join FoodDetail in db.FoodDetails
on OrderDetail.FoodID equals FoodDetail.FoodID
where OrderTable.DeliveredDateTime.Value.Month == month
select new
{
month = OrderTable.OrderedDateTime,
foodtitle = OrderDetail.FoodDetail.FoodTitle,
finalunitprice = OrderDetail.FinalUnitPrice,
quantity = OrderDetail.Quantity,
rawcost = FoodDetail.RawCost,
}).ToList();
var myquery = (from OrderTable in db.OrderTables
join OrderDetail in db.OrderDetails
on OrderTable.OrderID equals OrderDetail.OrderID
join FoodDetail in db.FoodDetails
on OrderDetail.FoodID equals FoodDetail.FoodID
where OrderTable.DeliveredDateTime.Value.Month == month
select new
{
month = OrderTable.OrderedDateTime,
foodtitle = OrderDetail.FoodDetail.FoodTitle,
finalunitprice = OrderDetail.FinalUnitPrice,
quantity = OrderDetail.Quantity,
rawcost = FoodDetail.RawCost,
});
foreach (var item in myquery)
{
rawCost += item.rawcost;
grossProfit += item.finalunitprice;
}
profit = grossProfit - rawCost;
lblTotalSales.Text = grossProfit.ToString();
lblTotalProfit.Text = profit.ToString();
}
}
请帮助我:(
答案 0 :(得分:1)
数据库类型为DateTime?
。因此,如果您确定该值不为null,则可以将其转换为DateTime
并使用其Month
值:
var myquery = (from OrderTable in db.OrderTables
join OrderDetail in db.OrderDetails
on OrderTable.OrderID equals OrderDetail.OrderID
join FoodDetail in db.FoodDetails
on OrderDetail.FoodID equals FoodDetail.FoodID
where ((DateTime)(OrderTable.DeliveredDateTime)).Month == month
select new
{
month = OrderTable.OrderedDateTime,
foodtitle = OrderDetail.FoodDetail.FoodTitle,
finalunitprice = OrderDetail.FinalUnitPrice,
quantity = OrderDetail.Quantity,
rawcost = FoodDetail.RawCost,
});