使用LINQ从数据库中选择数据时出现抑制状态错误CS0266 [英] Suppression State Error CS0266 When Selecting Data From Database Using LINQ
问题描述
我有一个Index视图,该视图显示了一个租户列表,并且效果很好.我在索引视图中添加了一个小表格,以接受查询字符串并将其传递给Tenants控制器的index方法.如您所见,我有一个If语句,用于检查查询字符串是否为空.如果不为空,它将进入并抓住一个租户,该租户的名字包含查询字符串的字符.好吧,这是我现在收到的错误.我相信这与我在ApplicatonUser模型中使用租户的ICollection的方式有关,或者与我首先将与登录用户相对应的租户加载到租户变量中的方式有关.我在下面添加了我的所有信息,以帮助诊断问题.
I have an Index view that shows a list of tenants and it works great. I added a small form to the index view to take in and pass a query string to the index method of my Tenants controller. As you can see, I have an If statement that checks to see if the query string is empty. If it is not empty, it goes in and grabs a tenant that has a first name that contains characters of the query string. Well, it is at this point I am receiving an error. I believe it has something to do with the way I am using the ICollection of Tenants in the ApplicatonUser Model or the way I am first loading the tenants that correspond to the logged in user into the tenants variable. I have added all my info below to help diagnose the issue.
错误消息
抑制状态错误CS0266无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'System.Collections.Generic.ICollection'.存在明确的转换(您是否缺少演员表?)
Suppression State Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.ICollection'. An explicit conversion exists (are you missing a cast?) mentorient
我的索引视图:
@model IEnumerable<mentorient.Models.Tenant>
@{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<nav>
<hr/>
<a asp-controller="Tenants" asp-action="New">New Tenant</a>
<hr/>
</nav>
@if (!Model.Any())
{
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Hold Up!</strong> You do not have any tenants yet.
</div>
}
else
{
<form>
<p>
Name: <input type="text" name="SearchString"/>
<input type="submit" value="Filter"/>
</p>
</form>
<table class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Options</th>
</tr>
</thead>
<tbody>
@foreach (var tenant in Model)
{
<tr>
<td>@tenant.Id</td>
<td>@tenant.FirstName @tenant.LastName</td>
<td><a asp-action="Delete" asp-route-id="@tenant.Id">Delete</a> | <a asp-action="Details" asp-route-id="@tenant.Id">Details</a></td>
</tr>
}
</tbody>
</table>
}
正在接受查询字符串的租户控制器的Index方法:
The Index method of the tenant controller that is accepting the query string:
public IActionResult Index(string searchString)
{
var userId = _userManager.GetUserId(User);
var tenants = _context.Users.Include(usr => usr.Tenants)
.Single(usr => usr.Id == userId)
.Tenants;
if (!String.IsNullOrEmpty(searchString))
{
tenants = tenants.Where(t => t.FirstName.Contains(searchString)); // this is where I am getting my error.
}
return View(tenants);
}
这里是租户模型:
namespace mentorient.Models
{
public class Tenant
{
public int Id { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Phone]
public int PhoneNumber { get; set; }
[Required]
public string Address { get; set; }
public string Email { get; set; }
[Required]
public DateTime DateOfBirth { get; set; }
[Required]
public string City { get; set; }
[Required]
public string ZipCode { get; set; }
[Required]
public string State { get; set; }
public string Country { get; set; }
}
}
我的ApplicationUser模型:
My ApplicationUser Model:
namespace mentorient.Models
{
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Tenant> Tenants { get; set; }
= new List<Tenant>();
}
}
推荐答案
您需要在查询末尾添加.ToList()
以选择租户列表,
You need to add .ToList()
at end of your query to select tenant list,
tenants.Where(t => t.FirstName.Contains(searchString)).ToList();
这篇关于使用LINQ从数据库中选择数据时出现抑制状态错误CS0266的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!