连接两个表实体框架
Posted
技术标签:
【中文标题】连接两个表实体框架【英文标题】:Joining two tables entity framework 【发布时间】:2017-01-29 18:07:09 【问题描述】:我正在尝试使用 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: '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,版本=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089],[System.String,mscorlib,版本=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089],[System .Nullable1[[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...' 不包含属性名为“idPredbiljezba”。
有人可以帮忙吗。
【问题讨论】:
能否分享一下GridView的代码? 为什么加入?别。你有一个导航属性PredbiljezbeEF.SeminariEF
。
我想过,但我不是很熟悉。你能写一些在这种情况下如何使用它的例子吗?
使用导航属性
嗯,x.SeminariEF.nazivSeminar
等
【参考方案1】:
从异常message => something
(可能是数据网格?)试图从您的gvPredbiljezbe
dataSource
访问名称为[idPredbiljezba]
的属性,但查询最终投影不包含您投影属性@987654325 的此类属性@如[id]
希望对你有帮助
【讨论】:
以上是关于连接两个表实体框架的主要内容,如果未能解决你的问题,请参考以下文章