【问题标题】:C# LINQ DateTime view profits by monthC# LINQ DateTime 按月查看利润
【发布时间】:2016-11-09 17:24:51
【问题描述】:

我想从一个下拉列表中按所选月份显示,该下拉列表将在 GRIDVIEW 中显示附有它们各自的 orderid、finalunitprice、rawcost 的食品标题,并根据累积计算总销售额finalunitprice和利润

但是我对数据库中的 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();


        }
    }

请帮助我:(

【问题讨论】:

  • 你试过month = OrderTable.OrderedDateTime.Month吗?
  • 我不清楚您的问题到底是什么。如果您只想要DateTime 的月份部分,则像在where 子句中那样使用Month 属性。也许你可以多解释一下你的确切问题是什么。
  • 我想按月显示销售的食品和利润,因此我很难编写 LINQ 语句,因为 DeliveredDateTime 的数据类型是 DateTime。

标签: c# asp.net linq datetime


【解决方案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,
                           });

【讨论】:

  • @juharr 我更正了我的代码并添加了解释。
  • 我不确定这是他们所说的 OP 的问题 我不确定如何转换 DateTime 以仅获取月份值 这对我来说表明month = OrderTable.OrderedDateTime 部分是错误的。
  • 是的,数据库中的类型是日期时间。我已根据您的帮助更新了代码,并更正了月份 = OrderTable.OrderDateTime.month 但是当我运行该站点时,除了 gridview 没有加载之外没有任何问题,我试图找出问题所在。 .
  • 好的,我在更新数据库中的一些数据后成功运行了代码。非常感谢!!
猜你喜欢
  • 2017-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-25
相关资源
最近更新 更多