Reputation: 260
I have this table:
and I want to display all friend
names are without repetitions by LINQ.
how do I do it?
Result is:
martin
kevin
igor
Controler:
dbEntities db = new dbEntities();
public ActionResult Index()
{
IQueryable<string> dn = from f in db.table select f.friend;
IQueryable<string> res = dn.Distinct();
return View(res);
}
View (ASP.NET MVC 3 Razor):
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.friend)
</td>
</tr>
}
Upvotes: 2
Views: 1249
Reputation: 3956
You can use a combination of Select
and Distinct
:
@foreach (var item in Model.Select(m => m.friend).Distinct()) {
<tr>
<td>
@item
</td>
</tr>
}
Upvotes: 5