三表1-M-1查询的问题
/// 会议名称
public class HY
{
public int HYID { get; set; }
public virtual List<HYDD> HYDD { get; set; }
}
/// 会议地点
public class HYDD
{
public int HYDDID { get; set; }
public int HYID { get; set; }
public int ZYID { get; set; }
public ZY ZY { get; set; }
}
///会议室资源
public class ZY
{
public int ZYID { get; set; }
public string Name { get; set; }
}
会议地点可空;
要求显示会议列表时,如果有会议地点显示出会议室名称,多个会议室名称用逗号分隔;
我这么做的
var tab = from s in db.HYs.Include(h => h.HYDD)
select s;
视图内@String.Join(",", item.HYDD.Select(s => s.ZY.Name))
提示“
未将对象引用设置到对象的实例。” 是ef关系没处理好?
改成
var q = from s in db.HYs
join o in (from e in db.HYDDs.Include(h => h.ZY)
select e) on s.HYID
equals o.HYID into os
select new
{
s,
a = string.Join(",", os.Select(t => t.ZY.Name))
};
对象类型又不对
------解决方案--------------------查找导航属性的方法
参考http://www.cnblogs.com/wlflovenet/archive/2011/07/29/EFandMvc6.html