Group by date and select statement

All I want to do is select all items from my Reviews table and group by date. My select statement at the end is breaking because 'review does not exist in the current context'. What am I doing wrong?

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();

Answers


At the end of the query, you have groups, not single values.So you can't do this review.Id,besides review is not exist anymore, you need to use g because you group them into 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() };

If this is the first time you use groupby, I would recommend you reading the documentation and see the examples: Group Elements in a Sequence


Here is what I ended up doing in my code behind file:

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();

And then on my aspx page:

<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>

Need Your Help

Stop Anyone From Viewing My Site Using an IFrame

php javascript iframe

I have looked and tried but don't see where I can stop some being able to browse my site through an IFrame or Thickbox?