I am doing a project and got confused in a session that to retrieve phone number from db and must visible only the latest phone number based on date time. That is I have two field in db called phonenumber and datetime. I have got a many repeated phone number in different datetime. so what I need is to show phone number only twice than many with condition that only the most latest date is used (don't want to show the first entered number in db). I have written code for group by but it is not working and I didn't get the correct code for my problem
public async Task<ActionResult> Index(string sortOrder, string CurrentSort, string currentFilter, string searchString, int? page, string FromDateString, string ToDateString)
{
ViewData["FromDateString"] = FromDateString;
ViewData["ToDateString"] = ToDateString;
int pageSize = 10; int pageIndex = 1;
pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;
ViewBag.CurrentSort = sortOrder;
sortOrder = String.IsNullOrEmpty(sortOrder) ? "DateTime" : sortOrder;
IPagedList<Phone> phones = null;
var List = db.Phones.OrderBy(t=>t.DateTime).ToList();
foreach(var group in List.GroupBy(item => item.phoneNo))
List = group.ToList();
}
if (!String.IsNullOrEmpty(searchString))
List = List.Where(s => s.phoneNo.ToString().Contains(searchString)).ToList();
switch (sortOrder)
case "DateTime": if (sortOrder.Equals(CurrentSort))
phones = List.OrderByDescending(m => m.DateTime).ToPagedList(pageIndex, pageSize); else
phones = List.OrderBy(m => m.DateTime).ToPagedList(pageIndex, pageSize);
break;
case "Default":
return View(phones);
<table class="table">
<tr>
<th>
phoneNo
</th>
<th> DateTime </th>
<th></th>
</tr>
@foreach (var item in Model)
<tr> <td> @Html.DisplayFor(modelItem => item.phoneNo) </td> <td>
@Html.DisplayFor(modelItem => item.DateTime) </td> <td>
@Html.ActionLink("Details", "Details", new { id = item.Id })
</td> </tr>
<td colspan="6"> <br />
<div style="text-align:left;"> Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount @Html.PagedListPager(Model, page => Url.Action("Index", new { page, currentFilter = ViewBag.CurrentFilter, sortOrder = ViewBag.sortOrder, ViewBag.FromDateString, ViewBag.ToDateString, ViewBag.searchString }))
</div> </td>
<td> <br />
</table>
this is the view page after going this it shows the actual means all the items from db and is not taking as group by
can anyone please help me to find the solution ???