I would like this ForEach to return h3 to me only once, instead of three which is the length of the array. How can I do? Thanks to those who will answer
@foreach (var winelistObj in Model.winetype.Where(s => s.name == Request.Query["wine"]))
{
<h3 >@winelistObj.name</h3>
}
CodePudding user response:
@foreach (var winelistObj in Model.winetype.Where(s => s.name == Request.Query["wine"]).FirstOrDefault())
{
<h3 >@winelistObj.name</h3>
}
This should give you the first result of the array.
CodePudding user response:
Maybe you want to concat all matching wines comma separated, then use string.Join
:
@{ string matchingWines = string.Join(",", Model.winetype.Where(s => s.name == Request.Query["wine"]).Select(w=> w.name)); }
<h3 >@matchingWines</h3>
You could add an if(!string.IsNullOrEmpty(matchingWines)) { ... }
so that you don't render the h3
if there was no matching wine or render a different message.