user
user

Reputation: 260

display without repetitions by LINQ-query

I have this table:

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

Answers (1)

Peter
Peter

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

Related Questions