无法将类型'System.Linq.IQueryable'隐式转换为'Microsoft.EntityFrameworkCore.Query.IIncludableQueryable'

时间:2018-05-19 08:59:00

标签: c# asp.net linq

当我开发ASP.Net App时,显示以下错误。

  

错误CS0266
  无法将类型'System.Linq.IQueryable'隐式转换为'Microsoft.EntityFrameworkCore.Query.IIncludableQueryable'有一个显式转换(检查是否缺少强制转换)urlapp12 C:\ Users \ mjkpt \ source \ repos \ teststage \ urlapp12 \ urlapp12 \ Controllers \ TagsController.cs 37 active

The captured screenshot of the error

.Urlid和Urlid都是int。

请有人帮助我。 提前谢谢。

包含错误的代码如下。

        //int id, [Bind("BlogId,Userid,Url,Title")] Blog blog
        // GET: Tags
//        public async Task<IActionResult> Index()
        public async Task<IActionResult> Index(int id, [Bind("Urlid,Userid,UrlStr,Title")] Url blog, int Urlid)
        {
            /*
            return View(await _context.Tag.ToListAsync());
            */
            var blogging02Context = _context.Tag.Include(t => t.Blog);

            if (!string.IsNullOrEmpty(Urlid.ToString()))
            {
                blogging02Context = blogging02Context.Where(t => t.Urlid == Urlid);
            }

            ViewBag.Urlid = Urlid;
            return View(await blogging02Context.ToListAsync());

//            return View (await _context.Tag.ToListAsync());
        }

Url.cs(Url Model)如下

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace urlapp12.Models
{
    public partial class Url
    {
        public Url()
        {
            Post = new HashSet<Post>();
        }

        [Key]
        public int Urlid { get; set; }
        public string UserId { get; set; }
        public string UrlStr { get; set; }
        public string Title { get; set; }

        public string CreatedBy { get; set; }
        public string CreatedBy_UserName { get; set; }
        [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
        public DateTime CreatedAt_UtcDt { get; set; }
        [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
        public DateTime CreatedAt_LocalDt { get; set; }

        public string LastUpdatedBy { get; set; }
        public string LastUpdatedBy_UserName { get; set; }
        [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
        public DateTime LastUpdatedAt_UtcDt { get; set; }
        [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
        public DateTime LastUpdatedAt_LocalDt { get; set; }

        public virtual ICollection<Tag> Tag { get; set; }
        public virtual ICollection<BlogIUDSqlHistory> BlogIUDSqlHistory { get; set; }
        public ICollection<Post> Post { get; set; }
        /*
        public List<Tag> Tag { get; set; }
        public List<Post> Post { get; set; }
        */
    }
}

Tag.cs​​如下。

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace urlapp12.Models
{
    public class Tag
    {
        [Key]
        public int TagId { get; set; }
        public int DispOrderNbr { get; set; }
        public string tagItem { get; set; }
        public int Urlid { get; set; }

        public string CreatedBy { get; set; }
        public string CreatedBy_UserName { get; set; }
        [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
        public DateTime CreatedAt_UtcDt { get; set; }
        [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
        public DateTime CreatedAt_LocalDt { get; set; }

        public string LastUpdatedBy { get; set; }
        public string LastUpdatedBy_UserName { get; set; }
        [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
        public DateTime LastUpdatedAt_UtcDt { get; set; }
        [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
        public DateTime LastUpdatedAt_LocalDt { get; set; }

        [System.ComponentModel.DataAnnotations.Schema.ForeignKey("Urlid")]
        public Url Blog { get; set; }
    }
}

(2018-05-19 18:17添加) Mohsin Mehmood先生,谢谢你的回复。 我已经尝试过您的代码,不幸的是发生了其他错误。

  

错误CS0266无法隐式转换Type   'System.Linq.IQueryable'成   'Microsoft.EntityFrameworkCore.DbSet'。有   一个明确的演员。 (你错过了演员?)     C:\ Users \ mjkpt \ source \ repos \ teststage \ urlapp12 \ urlapp12 \ Controllers \ TagsController.cs 45 Active

     

错误CS0266无法隐式转换Type   'Microsoft.EntityFrameworkCore.Query.IIncludableQueryable'成   'Microsoft.EntityFrameworkCore.DbSet'。有   一个明确的演员。 (你错过了演员?)     C:\ Users \ mjkpt \ source \ repos \ teststage \ urlapp12 \ urlapp12 \ Controllers \ TagsController.cs 45 Active

(2018-05-19 18:43已添加) user1672994先生,谢谢你的回复。

有效!非常感谢你 (有必要像下面这样做一点改变,但这不是一个大问题)

(更改前(user1672994的想法))         var blogging02Context = _context.Tag.Include(t =&gt; t.Blog).Where(t =&gt; t.Urlid == Urlid));

(更改后)         var blogging02Context = _context.Tag.Include(t =&gt; t.Blog).Where(t =&gt; t.Urlid == Urlid);

3 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。 Include在IQueryable之上产生一个新的抽象级别,称为IIncludableQueryable。您的var blogging02Context变为IIncludeQueryable,它不能直接从Where语句中分配。

将您的blogging02Context变量声明为IQueryable<Tag>而不是var。就我而言,这很有帮助。

        IQueryable<Tag> blogging02Context = _context.Tag.Include(t => t.Blog);

        if (!string.IsNullOrEmpty(Urlid.ToString()))
        {
            blogging02Context = blogging02Context.Where(t => t.Urlid == Urlid);
        }

答案 1 :(得分:1)

编译时错误正确,因为您已将var blogging02Context定义为_context.Tag.Include(....;此Include方法返回Microsoft.EntityFrameworkCore.Query.IIncludableQueryable类型。稍后,您要在where上添加blogging02Context条款,该条款会返回System.Linq.IQueryable

您可以使用以下代码更新代码:

,另一个点Urlid定义为int,因此此语句if (!string.IsNullOrEmpty(Urlid.ToString()))永远不会为假;因为int的默认值为 0 。并且0.ToString()将为“0”。

    public async Task<IActionResult> Index(int id, 
    [Bind("Urlid,Userid,UrlStr,Title")] Url blog, int Urlid)
    {
        var blogging02Context = _context.Tag.Include(t => t.Blog).Where(t => t.Urlid == Urlid));

        ViewBag.Urlid = Urlid;
        return View(await blogging02Context.ToListAsync());
    }

答案 2 :(得分:0)

请尝试以下代码:

 var blogging02Context = _context.Tag;

            if (!string.IsNullOrEmpty(Urlid.ToString()))
            {
                blogging02Context = blogging02Context.Where(t => t.Urlid == Urlid);
            }

            blogging02Context = blogging02Context.Include(t => t.Blog);

            ViewBag.Urlid = Urlid;