我有一张桌子
public class Product
public int Id { get; set; }
public string Model_Name { get; set; }
public string Company_Name { get; set; }
public Nullable<int> Price { get; set; }
public string Photo { get; set; }
现在该表包含多个项目,所以我只想要检索公司的名称并进行调度。但是,Company_Name列包含重复值。我如何检查它是否包含重复的名称,然后只显示一次值。我想在视图中检查它。这是视图。
@foreach (var item in Model)
{
<tr>
//Anything extra could be added here to check the duplicate values
<td>@Html.ActionLink(item.Company_Name, "Brand",
new { Company_Name = item.Company_Name })
</td>
</tr
}
Here is how the table looks like
Id Model_Name Company_Name price photo
1 S7862 Samsung 1500 something
2 Galaxy Samsung 1500 something
3 Apple Apple 2000 something
4 Desire HTC 1500 something
5 Desire X2 HTC 1500 something
6 Nokia Lumia Nokia 1500 something
现在在上表中缺少一些记录,如果我写
@foreach(var item in Model)
{
<tr>
<td>
@Html.ActionLink(item.Company_Name, "Brand", new {Company_Name=item.Company_Name})
</td>
</tr>
}
现在我在这里得到结果
Brand
Samsung
Samsung
Apple
HTC
HTC
Nokia
我希望它像
一样Brand
Samsung
Apple
HTC
Nokia
任何想法将不胜感激。我希望Linq能够进入视野。
答案 0 :(得分:1)
如何在Company_Name
上对它们进行分组:
@{
var grouped = Model.GroupBy(x=>x.Company_Name);
}
@foreach (var item in grouped)
{
<tr>
//Anything extra could be added here to check the duplicate values
<td>@Html.ActionLink(item.Key"Brand",
new { Company_Name = item.Key })
</td>
</tr
}
答案 1 :(得分:1)
尝试在该属性上使用Distinct():
@foreach (var companyName in Model.Select(m => m.Company_Name).Distinct())
{
<tr>
//Anything extra could be added here to check the duplicate values
<td>@Html.ActionLink(companyName, "Brand",
new { Company_Name = companyName })
</td>
</tr
}