如何执行两个集合的连接并填充导航属性

时间:2015-05-26 20:53:45

标签: c# asp.net linq entity-framework linq-to-entities

假设我有两个课程,TeapotCup

public class Teapot
{
    [Key]
    [Column("Id", TypeName = "int")]
    public int Id         { get; set; }

    public int MaterialId { get; set; }

    public int ColorId    { get; set; }

    [ForeignKey("MaterialId")]
    public virtual Material Material { get; set; }
    [ForeignKey("ColorId")]
    public virtual Color    Color    { get; set; }
}

public class Cup
{
    [Key]
    [Column("Id", TypeName = "int")]
    public int Id         { get; set; }

    public int MaterialId { get; set; }

    public int ColorId    { get; set; }

    [ForeignKey("MaterialId")]
    public virtual Material Material { get; set; }
    [ForeignKey("ColorId")]
    public virtual Color    Color    { get; set; }
}

这是我的ViewModel:

namespace ViewModel.Data
{
    public class TeapotsWithInfo
    {
        public Model.Data.Teapot Teapot { get; set; }
        public Model.Data.Cup    Cup    { get; set; }
    }
}

对于我的ViewModel,我需要在MaterialId和ColorId上执行连接,并包含一些导航功能,例如Teapot.Material.Manufacturer。所以,我尝试过以下查询:

  1. 抛出“LINQ to Entities仅支持转换EDM原语或枚举类型”

    (from t in db.Teapots
     join c in db.Cups
     on new { t.MaterialId, t.ColorId } equals new { c.MaterialId, c.ColorId }
     where t.Id == id
     select new ViewModel.Data.TeapotsWithInfo { Teapot = t, Cup = c })
        .Include(t => t.Material.Manufacturer).SingleOrDefault();
    
  2. 这似乎忽略了Include

     (from t in db.Teapots.Include(t => t.Material.Manufacturer)
     join c in db.Cups
     on new { t.MaterialId, t.ColorId } equals new { c.MaterialId, c.ColorId }
     where t.Id == id
     select new ViewModel.Data.TeapotsWithInfo { Teapot = t, Cup = c }).SingleOrDefault();
    
  3. 现在我在这里找到一些答案,建议枚举然后执行另一个选择,但我宁愿一次性捕获数据。

1 个答案:

答案 0 :(得分:1)

您在导航属性方面遇到了困难,因为带有连接或投影的查询会忽略急切加载(请参阅this)。

不幸的是,您需要执行您似乎要避免的操作:首先从数据库中提取数据,然后再执行一次选择以构建ViewModel(将从原始查询中加载关系)。但是,附加选择应该是一个相当简单的操作,因为您没有从数据库执行额外的加载,并且可枚举应该只包含一个项目。

(from t in db.Teapots.Include(t => t.Material.Manufacturer)
 join c in db.Cups
 on new { t.MaterialId, t.ColorId } equals new { c.MaterialId, c.ColorId }
 where t.Id == id
 select new 
  {
     Teapot = t,
     Cup = c,
     Material = t.Material,
     Manufacturer = t.Material.Manufacturer,
  })
.AsEnumerable()
.Select(a => new ViewModel.Data.TeapotsWithInfo 
  { 
     Teapot = a.Teapot, 
     Cup = a.Cup 
  })
.SingleOrDefault();

<强>来源