无法施放类型的对象' collections.generic.list`1 [microsoft.entityframeworkcore.query.iincludablequeryable`2

发布于 2025-01-21 06:55:28 字数 2269 浏览 2 评论 0原文

我正在尝试从查询中添加类的属性。我有两个类别的“文章”,以及与Manuttomany关系的来源。 资料来源:

 public class Source
    {
        public Source()
        {
            this.Id = Guid.NewGuid();
        }

        public Guid Id { get; set; }
        public int ApiId { get; set; }

        public string? FullName { get; set; }
        public string? Bio { get; set; }

        public virtual ICollection<Article> Articles { get; set; }

                }

文章:

 public class Article
    {
        public Article()
        {
            this.Id = Guid.NewGuid();
        }

        public Guid Id { get; set; }
        public int ApiId { get; set; }

        public string Title { get; set; }
        public string Summary { get; set; }

        public double Rating { get; set; }

        public DateTime? AirDate { get; set; }

        public virtual ICollection<Source> Cast { get; set; }

    }

在我的方法中,首先我进行拆分然后查询。我想将查询结果添加到我的类文章的属性中,

 public void AddArticle(string name, string summary = null, string sources = " ", double rating = 5, DateTime? airDate = null)
        {
            using (var db = this._dataContextFactory.Create())
            {

                var sourcesDb = sources.Split(',').Select(a => db.Sources.Where<Source>(s => s.FullName == a).Include(s => s.Articles)).ToList();

                db.Articles.Add(new Article()
                {
                    Cast = (ICollection<Source>)sourcesDb.ToList(),
                    AirDate = airDate,
                    Rating = rating,
                    Summary = summary,
                    Title = name
                }); ; 
                
                db.SaveChanges();
                }
           
        }

当我执行代码时,我有以下错误:

exception {“无法施放type'system.collections.collections.generic.generic.list1 [Microsoft.entityFrameWorkCore.Query.iincludableQueryable 2 [test1project.models.models.source,System.Collections.generic.generic.generic.icollection 1 [test1project.models.models.models.models.models.models.models.tilection]]] generic.icollection 1 [test1project.models.source]'。“} system.invalidcastException

谢谢您的帮助

I am trying to add a property of a class from a query. I have two classes "Article & Source with manytomany relationship.
source:

 public class Source
    {
        public Source()
        {
            this.Id = Guid.NewGuid();
        }

        public Guid Id { get; set; }
        public int ApiId { get; set; }

        public string? FullName { get; set; }
        public string? Bio { get; set; }

        public virtual ICollection<Article> Articles { get; set; }

                }

article :

 public class Article
    {
        public Article()
        {
            this.Id = Guid.NewGuid();
        }

        public Guid Id { get; set; }
        public int ApiId { get; set; }

        public string Title { get; set; }
        public string Summary { get; set; }

        public double Rating { get; set; }

        public DateTime? AirDate { get; set; }

        public virtual ICollection<Source> Cast { get; set; }

    }

in my method, first I do a split then a query. I want to add the results of the query to the property Cast of my class Article

 public void AddArticle(string name, string summary = null, string sources = " ", double rating = 5, DateTime? airDate = null)
        {
            using (var db = this._dataContextFactory.Create())
            {

                var sourcesDb = sources.Split(',').Select(a => db.Sources.Where<Source>(s => s.FullName == a).Include(s => s.Articles)).ToList();

                db.Articles.Add(new Article()
                {
                    Cast = (ICollection<Source>)sourcesDb.ToList(),
                    AirDate = airDate,
                    Rating = rating,
                    Summary = summary,
                    Title = name
                }); ; 
                
                db.SaveChanges();
                }
           
        }

when I execute the code I have the following error :

exception {"Unable to cast object of type 'System.Collections.Generic.List1[Microsoft.EntityFrameworkCore.Query.IIncludableQueryable2[test1Project.Models.Source,System.Collections.Generic.ICollection1[test1Project.Models.Article]]]' to type 'System.Collections.Generic.ICollection1[test1Project.Models.Source]'."} System.InvalidCastException

Thank you for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙气飘飘 2025-01-28 06:55:28

谢谢,我编辑了我的方法
var arrayofstring = sources.split(','); var sourcesdb = db.sources.where(s =&gt; arrayofstring.contains(s.fullname))。include(s =&gt; s. s. articles).tolist();>
而且有效。再次感谢 -

Thank you , I edited my method to
var arrayOfString = sources.Split(','); var sourcesDb =db.Sources.Where(s => arrayOfString.Contains(s.FullName)).Include(s => s.Articles).ToList();
and it works . Thanks again –

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文