当我开发我的 ASP.Net 应用程序时,显示以下错误。


t.UrlidUrlid 都是 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 模型 Url.cs 如下:
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 模型 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 代码,不幸的是发生了其他错误。



(2018-05-19 18:43 已添加)

成功了! 非常感谢 user1672994
: 有必要做如下的一点改动,不过问题不大:

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

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

最佳答案

编译时错误是正确的,因为您在第一行将 var blogging02Context 定义为 _context.Tag.Include(.... ;此 Include 方法返回 Microsoft.EntityFrameworkCore.Query.IIncludableQueryable 类型。稍后,您将在返回 whereblogging02Context 上添加 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());
    }

关于c# - 无法将类型 'System.Linq.IQueryable' 隐式转换为 'Microsoft.EntityFrameworkCore.Query.IIncludableQueryable',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50423538/

10-10 13:47