如果 mvc4 中已经存在重复值,则使用 linq 选择单个值
Selecting a single value using linq if already duplicate values exist in mvc4
我有一个table
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; }
现在 table 包含多个项目,所以我只想检索公司名称并显示它。但是 Company_Name 列包含重复值。我需要如何检查它是否包含重复名称,然后还只显示值 once.I 想在视图中检查它。这是视图。
@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
现在上面table少了一些记录,如果我写
@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 进入视图。
如何在 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
}
尝试在 属性:
上使用 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
}
我有一个table
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; }
现在 table 包含多个项目,所以我只想检索公司名称并显示它。但是 Company_Name 列包含重复值。我需要如何检查它是否包含重复名称,然后还只显示值 once.I 想在视图中检查它。这是视图。
@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
现在上面table少了一些记录,如果我写
@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 进入视图。
如何在 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
}
尝试在 属性:
上使用 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
}