【发布时间】:2015-05-26 20:53:44
【问题描述】:
假设我有两个班级,Teapot 和 Cup:
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。所以,我尝试了以下查询:
-
这会抛出“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(); -
这似乎忽略了
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();
现在我在这里找到了一些建议枚举然后执行另一个选择的答案,但我宁愿一次性捕获数据。
【问题讨论】:
-
“一次”是什么意思?如果你枚举然后选择,你只会发送一个数据库查询。
标签: c# asp.net linq entity-framework linq-to-entities