按日期分组并选择对账单



我想做的就是从我的Reviews表中选择所有项目并按日期分组。我最后的select语句正在中断,因为"当前上下文中不存在审阅"。我做错了什么?

var byDate = from review in myEntities.Reviews
             let date = review.CreateDateTime
             group review by EntityFunctions.TruncateTime(date) into g
             orderby g.Key
             select new { review.Id, review.CreateDateTime, review.Summary, review.Body };
repeater1.DataSource = byDate;
repeater1.DataBind();

在查询结束时,您有,而不是单个值。所以你不能这样做review.Id,除了审查已经不存在之外,你需要使用g,因为你将它们分组到g中。

from review in myEntities.Reviews
let date = review.CreateDateTime
group review by EntityFunctions.TruncateTime(date) into g
orderby g.Key
select new { Date = g.Key, Rewiews = g.ToList() };

如果这是您第一次使用groupby,我建议您阅读文档并查看示例:在序列中分组元素

以下是我在代码隐藏文件中完成的操作:

var byDate = (from review in myEntities.Reviews
              group review by review.CreateDateTime into dateGroup
              select new
              {
                 dateGroup.Key,
                 Reviews = from review in myEntities.Reviews
                           where review.CreateDateTime.Day == dateGroup.Key.Day &&
                           review.CreateDateTime.Month == dateGroup.Key.Month &&
                           review.CreateDateTime.Year == dateGroup.Key.Year   
                           select new { review.CreateDateTime, review.Title }
               });
Repeater1.DataSource = byDate;
Repeater1.DataBind();

然后在我的aspx页面上:

<asp:Repeater ID="Repeater1" runat="server">
   <ItemTemplate>
      <asp:Literal ID="Literal1" runat="server" Text='<%# Eval("Key", "{0:MM/dd/yy}") %>' />
      <asp:BulletedList ID="ReviewList" runat="server" DataSource='<%# Eval("Reviews") %>' />
  </ItemTemplate>
</asp:Repeater>

相关内容

  • 没有找到相关文章

最新更新