【问题标题】:Adding ICollection in Linq using three models使用三个模型在 Linq 中添加 ICollection
【发布时间】:2014-03-06 21:07:36
【问题描述】:

您好,我有三个表的实体,这些表是类

 public partial class pro_type
    {
        public pro_type()
        {
            this.pro = new HashSet<pro>();
        }

        public int pro_id { get; set; }
        public string description { get; set; }    
        public virtual ICollection<pro> pro { get; set; }
    }

 public partial class pro
    {
        public pro()
        {
            this.project_discussion = new HashSet<p_dis>();
        }

        public int pro_id { get; set; }
        public string name { get; set; }
        public System.DateTime ts { get; set; }
        public int p_type_id { get; set; }  
        public virtual ICollection<p_disc> p_disc { get; set; }
        public virtual ICollection<p_type> p_type { get; set; }
    }
}

  public partial class p_dis
    {
        public int p_disc_id { get; set; }
        public int pro_id { get; set; }
        public string message { get; set; }      
        public virtual pro pro { get; set; }
    }
}

我有以下 linq 语句从这三个类中创建一个对象。

  var pView= (from c in db.p_disc
                               select new pView
                               {
                                 name=c.pro.name,
                                 pro_id=c.pro.pro_id,                           
                                 message=c.message,
                                 pro_type=typeof(ICollection<c.pro.p_type>)
                               }).Take(7);
            return pView;

我一直在 pro_type=typeof(ICollection) 说.. “无法将类型 system.Type 隐式转换为 system.collections.generic.icollection”

请告诉我如何解决它。谢谢

【问题讨论】:

    标签: c# linq entity-framework asp.net-web-api


    【解决方案1】:

    错误很明显。

    typeof(ICollection) 不会创建 ICollection - 它会为您提供一个 System.Type 对象,其中包含您的类型的详细信息。

    System.Type 类的 MSDN:
    http://msdn.microsoft.com/en-us/library/system.type(v=vs.110).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 2022-12-15
      • 2018-11-01
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多