抑制状态错误CS0266使用LINQ从数据库中选择数据时[英] Suppression State Error CS0266 When Selecting Data From Database Using LINQ

本文是小编为大家收集整理的关于抑制状态错误CS0266使用LINQ从数据库中选择数据时的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个索引视图,显示租户列表,并且效果很好.我在索引视图中添加了一个小表格,以接收并将查询字符串传递给我租户控制器的索引方法.如您所见,我有一个IF语句检查查看查询字符串是否为空.如果它不是空的,它会进入并抓住一个包含查询字符串字符的名字的租户.好吧,在这一点上,我正在收到一个错误.我相信这与我使用Applicatonuser模型中租户的偶像的方式有关,或者我首先将与登录用户相对应的租户加载到租户变量中.我在下面添加了所有信息,以帮助诊断问题.

错误消息

抑制状态错误CS0266不能隐式将类型'System.Collections.generic.ienumerable'转换为'System.Collections.generic.icollection'.存在明确的转换(您是否错过了演员?)指导

我的索引视图:

@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">&times;</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>
}

接受查询字符串的租户控制器的索引方法:

 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; }
    }
}

我的应用程序器模型:

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()以选择租户列表,

tenants.Where(t => t.FirstName.Contains(searchString)).ToList();

本文地址:https://www.itbaoku.cn/post/1557223.html

问题描述

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.

Error Message

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

My Index View:

@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">&times;</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>
}

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);
        }

Here is the Tenant Model:

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; }
    }
}

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>();
        }
    }

推荐答案

You need to add .ToList() at end of your query to select tenant list,

tenants.Where(t => t.FirstName.Contains(searchString)).ToList();
相关标签/搜索