加入两个表实体框架

时间:2016-09-21 13:05:35

标签: c# asp.net entity-framework gridview

我正在尝试使用LINQ

在实体框架中连接两个表

以下是模型:

    public partial class PredbiljezbeEF
    {
        public int idPredbiljezba { get; set; }
        public Nullable<System.DateTime> datumPredbiljezba { get; set; }
        public string imePolaznik { get; set; }
        public string prezimePolaznik { get; set; }
        public string gradPolaznik { get; set; }
        public string adresaPolaznik { get; set; }
        public Nullable<int> postanskiBrojPolaznik { get; set; }
        public Nullable<bool> statusPredbiljezba { get; set; }
        public int idSeminar { get; set; }

        public virtual SeminariEF SeminariEF { get; set; }
    }

        public partial class SeminariEF
    {

        public SeminariEF()
        {
            this.PredbiljezbeEFs = new HashSet<PredbiljezbeEF>();
        }

        public int idSeminar { get; set; }
        public string nazivSeminar { get; set; }
        public string opisSeminar { get; set; }
        public string datumSeminar { get; set; }
        public Nullable<bool> statusSeminar { get; set; }
        public Nullable<int> brojPredbiljezbi { get; set; }

这是LINQ表达式:

        if(!IsPostBack)
        {
            SeminariEntities db = new SeminariEntities();

            var predB = (from x in db.PredbiljezbeEFs
                         join y in db.SeminariEFs on x.idSeminar equals y.idSeminar
                         select new
                         {
                             id = x.idPredbiljezba,
                             ime = x.imePolaznik,
                             prez = x.prezimePolaznik,
                             datum = x.datumPredbiljezba,
                             grad = x.gradPolaznik,
                             adresa = x.adresaPolaznik,
                             post = x.postanskiBrojPolaznik,
                             status = x.statusPredbiljezba,
                             naziv = y.nazivSeminar,
                             datumS = y.datumSeminar

                         }).ToList();







            //var predB = (from x in db.PredbiljezbeEFs select x).ToList();

            gvPredbiljezbe.DataSource = predB.ToList();
            gvPredbiljezbe.DataBind();


        }

我收到此错误:

  

DataBinding:&#39;&lt;&gt; f__AnonymousType0 10[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Nullable 1 [[System.DateTime,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]],mscorlib,Version = 4.0。 0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral, PublicKeyToken = b77a5c561934e089],[System.Nullable 1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Nullable 1 [[System.Boolean,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]],mscorlib,Version = 4.0.0.0,Culture = n ...&#39;不包含名称为&#39; idPredbiljezba&#39;的媒体资源。

有人可以帮忙。

1 个答案:

答案 0 :(得分:0)

异常message => something(数据网格可能是?)正在尝试从[idPredbiljezba] gvPredbiljezbe访问名为dataSource的媒体资源,但查询最终投影不会包含这样的属性,您将属性[idPredbiljezba]投影为[id]

希望有所帮助