我正在为一项任务开发一个blazor网页,您可以在其中添加、查看和搜索存储在json文件中的成人对象。我现在正在做搜索功能,遇到了一个问题。我试图创建一个IEnumerable查询,它从成年人的完整列表开始,然后根据每个搜索条件进行检查,如果条件不为空,它会将查询缩小到符合该条件的成年人列表。出于某种原因,在检查第一个条件后,查询的值从42(成人列表的长度(更改为:{System.Linq.Enumerable.WhereListIterator<Assignment_1.Models.adult>}
我该如何解决这个问题,所以最终剩下的只是一份符合所有输入标准的成年人名单?
@inject Assignment_1.Models.AdultManager adultManager;
@page "/search"
<h3>Search for adults</h3>
<form>
<label for="firstname">First name</label>
<input type="text" id="firstname" @bind-value="firstName">
<label for="lastname">Last name</label>
<input type="text" id="lastname" @bind-value="lastName"><br>
<label for="haircolor">Haircolor</label>
<select id="haircolor" @bind="hairColor">
<option disabled selected value></option>
<option value="blonde">Blonde</option>
<option value="red">Red</option>
<option value="brown">Brown</option>
<option value="black">Black</option>
<option value="white">White</option>
<option value="grey">Grey</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>
<label for="eyecolor">Eyecolor</label>
<select id="eyecolor" @bind="eyeColor">
<option disabled selected value></option>
<option value="brown">Brown</option>
<option value="black">Black</option>
<option value="grey">Grey</option>
<option value="blue">Blue</option>
<option value="amber">Amber</option>
<option value="hazel">Hazel</option>
</select><br>
<label for="height">Height</label>
<input type="text" id="height" @bind-value="height">
<label for="weight">Weight</label>
<input type="text" id="weight" @bind-value="weight"><br>
<label for="age">Age</label>
<input type="text" id="age" @bind-value="age">
<label>Sex</label>
<input type="radio" id="m" name="gender" value="M" @onchange="genderSelected">
<label for="m">M</label>
<input type="radio" id="f" name="gender" value="F" @onchange="genderSelected">
<label for="f">F</label>
<button type="button" @onclick="search">Search</button>
</form>
<table class="table">
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>ID</th>
<th>Haircolor</th>
<th>Eyecolor</th>
<th>Age</th>
<th>Weight</th>
<th>Height</th>
<th>Job</th>
<th>Sex</th>
</tr>
</thead>
<tbody>
@if (searchedAdult!=null)
{ foreach (Assignment_1.Models.Adult adult in searchedAdult)
{
<tr>
<td>@adult.FirstName</td>
<td>@adult.LastName</td>
<td>@adult.Id</td>
<td>@adult.HairColor</td>
<td>@adult.EyeColor</td>
<td>@adult.Age</td>
<td>@adult.Weight</td>
<td>@adult.Height</td>
<td>@adult.JobTitle</td>
<td>@adult.Sex</td>
</tr>
}
}
</tbody>
</table>
@code {
public string firstName;
public string lastName;
public string hairColor;
public string eyeColor;
public string weight;
public string height;
public string jobTitle;
public string age;
public string sex;
public List<Models.Adult> searchedAdult;
private void search()
{
IEnumerable<Models.Adult> query = adultManager.getAllAdults();
if (firstName != null || firstName!="")
{
query = query.Where(x => x.FirstName == firstName);
}
if (lastName != null || lastName!="")
{
query = query.Where(x => x.LastName==lastName);
}
if (hairColor != null || hairColor!="")
{
query = query.Where(x => x.HairColor == hairColor);
}
if (eyeColor != null || eyeColor != "")
{
query = query.Where(x => x.EyeColor == eyeColor);
}
if (weight != null || weight != "")
{
query = query.Where(x => x.Weight.ToString() == weight);
}
if (height != null || height != "")
{
query = query.Where(x => x.Height.ToString() == height);
}
if (jobTitle != null || jobTitle != "")
{
query = query.Where(x => x.JobTitle == jobTitle);
}
if (age != null || age != "")
{
query = query.Where(x => x.Age.ToString() == age);
}
if (sex != null || sex != "")
{
query = query.Where(x => x.Sex == sex);
}
searchedAdult = query.ToList();
}
private void genderSelected(ChangeEventArgs a)
{
sex= a.Value.ToString();
}
}
由于某些原因,在检查第一个条件后,查询的值从42(成人列表的长度(更改为:{System.Linq.Enumerable.WhereListIterator<Assignment_1.Models.adult>}
query = query.Where(x => x.FirstName == firstName);
这不会执行查询,它只定义了一个查询。
{System.Linq.Enumerable.WhereListIterator<Assignment_1.Models.Adult>}
例如,要执行,需要调用ToList()
。
旁注:if (firstName != null || firstName!="")
可以写成
if(!String.IsNullOrEmpty(firstname))
在C#9.0中当前在rc2 中
if (string.IsNullOrEmpty(firstname) is not true)
这是一个编码风格的东西。。。
@if (searchedAdult!=null)
{ foreach (Assignment_1.Models.Adult adult in searchedAdult)
{
<tr>
你也应该在这里使用@key。
@foreach(var adult in searchedAdult ?? Array.Empty<Adult>())
{
<tr @key="adult">