Suppression State Error CS0266 When Selecting Data From Database Using LINQ

大城市里の小女人 提交于 2020-01-06 05:50:30

问题


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

回答1:


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

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


来源:https://stackoverflow.com/questions/48237029/suppression-state-error-cs0266-when-selecting-data-from-database-using-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!