I have a fairly simple model:
public class Delivery
{
public int DeliveryId { get; set; }
public int OverId { get; set; }
public int Ball { get; set; }
public int Runs { get; set; }
public Player Player { get; set; }
}
All I want to do is have my collection of Delivery objects grouped by Player so that I can then perform some stats calculations on the results in my MVC3 view.
I’m almost there, but between the L2S query and my model binding declaration, I just can’t get the two to marry up.
Doing it this way almost works:
var batting = from d in deliveries
where d.Over.IsBatting == true
group d by d.Player into player
select player;
return View(batting);
But the view bindings are a mess.
Bit of help?
EDIT:
Here’s my view:
@model IEnumerable<IGrouping<Cricket.Models.Player, Cricket.Models.Delivery>>
@{
ViewBag.Title = "Batting";
}
<h2>Batting</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th></th>
<th>
OverId
</th>
<th>
Ball
</th>
<th>
Runs
</th>
</tr>
@foreach (var Item in Model) {
<tr>
<td>
@* @Html.ActionLink("Edit", "Edit", new { id=item.DeliveryId }) |
@Html.ActionLink("Details", "Details", new { id=item.DeliveryId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.DeliveryId })
*@ </td>
<td>
@Item.Key
</td>
<td>
@*item.Ball *@
</td>
<td>
@Item.Sum(x => x.Runs)
</td>
</tr>
}
</table>
You can create a ViewModel like
and you can modify your query little bit like
Now you can bind this viewmodel to your view rather than complex clumsy grouping.
*Note:*There may be some syntax errors in query but this is whole idea of projecting grouping to your viewmodels