【发布时间】:2022-11-13 23:58:48
【问题描述】:
@if (items != null)
{
<select required class="col-sm-10" bind="searchByLocation" @onchange="LocationOnChange">
<option value="" disabled selected hidden>Search By Location</option>
@foreach (var r in items)
{
<option value="@r.location">@r.location</option>
}
</select>
}
下面是我的课程和工作代码,在下拉列表中显示位置(但不是唯一值)。
public class DisplayItem
{
public string itemKey { get; set; }
public string id { get; set; }
public string location{ get; set; }
}
List<DisplayItem> items = new ();
private DisplayItem[] locationDropDown ;
protected override async Task OnInitializedAsync()
{
items = (await ItemService.GetItems())
.GroupBy(x => x.ItemKey)
.Select(x => new DisplayItem
{
ItemKey = x.Key,
Id = x.FirstOrDefault(y => y.Key == "Id")?.Value,
Location = x.FirstOrDefault(y => y.Key == "Location")?.Value,
})
.ToList();
// locationDropDown = items.Select(x => x.location).Distinct().ToArray();
}
下面通过从下拉列表中选择位置来帮助过滤记录。但是多次显示同一个国家。我只想在下拉列表中显示唯一值。谢谢你。
private DisplayItem[] result;
string searchByLocation;
private async Task LocationOnChange(ChangeEventArgs args)
{
searchByLocation = (string)args.Value;
await LocationChanged();
}
private async Task LocationChanged()
{
result= items.Where(part => part.location == searchByLocation).ToArray();
}
【问题讨论】:
-
为什么 Distinct() 不适合你?为什么你注释掉这条线?