2012-04-21 43 views
2

重复我有这个表:显示,而不通过LINQ查询

table

,我要显示所有friend名称是不被重复LINQ。 我该怎么做? 结果是:

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

视图(ASP.NET MVC 3剃须刀):

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.friend) 
     </td> 
    </tr> 
} 

回答

5

您可以使用SelectDistinct组合:

@foreach (var item in Model.Select(m => m.friend).Distinct()) { 
    <tr> 
     <td> 
      @item 
     </td> 
    </tr> 
} 
+0

感谢名单!它的工作,但它没有任何显示,我改变@ Html.Display(Item)为@ Html.ActionLink(item,“...”) – user 2012-04-21 15:47:09

+0

我将输出从'Display ...'更改为朋友名称(if你不想显示这个名字作为链接)。 – Peter 2012-04-21 15:51:24

+0

thx,但我需要链接=) – user 2012-04-21 15:56:34