如何实现实体框架代码首先加入 [英] How to implement Entity Framework Code First join

查看:97
本文介绍了如何实现实体框架代码首先加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用实体框架代码。



假设有这样的POCO(最终简化):

  public BortStructure类
{
public Guid Id {get;组; }
public String Name {get;组;
}

public class Slot
{
public Guid Id {get;组; }
public String Name {get;组; }
public BortStructure {get;组; }
}

public class SystemType
{
public Guid Id {get;组; }
public String Name {get;组; }
}

public class SlotSystemType
{
public Guid Id {get;组; }
public Slot Slot {get;组; }
public SystemType SystemType {get;组;
}

和上下文

  public MyContext:DbContext 
{
public DbSet< BortStructure> BortStructures {get;组; }
public DbSet< Slot>老虎机{get;组; }
public DbSet< SystemType> SystemTypes {get;组; }
public DbSet< SlotSystemType> SlotSystemTypes {get;组;
}

我有一个任务可以通过Id列出附带的插槽来获取BortStructure,每个都附有systemTypes列表。



使用SQL可以让我用一些JOIN来实现:

  SELECT BortStructures.Id,BortStructures.Name,Slots.Id,
Slots.Name,SystemType.Id,SystemType.Name FROM
((BortStructures LEFT JOIN Slots ON BortStructures。 Id = Slots.BortStructureId)
LEFT JOIN SlotSystemTypes ON SlotSystemTypes.SlotId = Slots.Id)
LEFT JOIN SystemTypes ON SystemTypes.Id = SlotSystemTypes.SystemTypeId
WHERE BortStructures.Id ='XXXXXX'ORDER BY Slots.Id,SystemType.Id

但是实体框架代码首先我没有任何想法如何使用



如果我使用

  var slotSystemTypes = from sl在MyContext.SlotSystemTypes 
其中sl.Slot.BortStructure.Id = XXXXXX
orderby sl.Slot.Id,sl.SystemType.Id
select sl;

当然,如果BortStructure由没有附加任何SystemTypes的Slots / Slots组成,我将不会收到任何内容。



而不是使用空插槽/插槽的空列表获得BortStructure,每个都附有我希望获得的SystemTypes的空列表。



有没有办法使用单一 LINQ查询来存档我的数据库配置?

解决方案

您可以使用 join 运算符示例:

  string [] category = new string [] {
饮料,
调味品,
蔬菜,
乳制品,
海鲜} ;

列表<产品> products = GetProductList();

var q =
从c在类别
中连接p在c上的产品等于p.Category
select new {Category = c,p.ProductName};

foreach(var v in q)
{
Console.WriteLine(v.ProductName +:+ v.Category);
}

更多示例: http://code.msdn.microsoft.com/LINQ-Join-Operators-dabef4e9


I'm starting to use Entity Framework Code First.

Suppose to have such POCO's (ultimately simplified):

public class BortStructure
{
   public Guid Id { get; set; }
   public String Name { get; set; }
}

public class Slot
{
   public Guid Id { get; set; }
   public String Name { get; set; }
   public BortStructure { get; set; }
}

public class SystemType
{
   public Guid Id { get; set; }
   public String Name {get; set; }
}

public class SlotSystemType
{
   public Guid Id { get; set; }
   public Slot Slot { get; set; }
   public SystemType SystemType {get; set; }
}

and a context

public MyContext : DbContext
{
   public DbSet<BortStructure> BortStructures { get; set; }
   public DbSet<Slot> Slots{ get; set; }
   public DbSet<SystemType> SystemTypes { get; set; }
   public DbSet<SlotSystemType> SlotSystemTypes { get; set; }
}

I have a task to get BortStructure by Id with list of attached Slots, each one with list of systemTypes attached.

Using SQL allowed me to do that with some JOIN's:

SELECT BortStructures.Id, BortStructures.Name, Slots.Id, 
Slots.Name, SystemType.Id, SystemType.Name FROM
((BortStructures LEFT JOIN Slots ON BortStructures.Id = Slots.BortStructureId)
LEFT JOIN SlotSystemTypes ON SlotSystemTypes.SlotId = Slots.Id)
LEFT JOIN SystemTypes ON SystemTypes.Id = SlotSystemTypes.SystemTypeId
WHERE BortStructures.Id='XXXXXX' ORDER BY Slots.Id, SystemType.Id

But with Entity Framework Code First I don't have any idea howto do that.

If I use

var slotSystemTypes = from sl in MyContext.SlotSystemTypes
where sl.Slot.BortStructure.Id = XXXXXX
orderby sl.Slot.Id, sl.SystemType.Id
select sl;

i, of course, will receive nothing if BortStructure consists of no Slots/Slots without any SystemTypes attached.

Instead of getting BortStructure with empty list of Slots/with Slots, each one with empty list of SystemTypes attached as I expect to get.

Is there any way to archive that with single LINQ query for my database configuration?

解决方案

You can use join operator example:

string[] categories = new string[]{  
    "Beverages",   
    "Condiments",   
    "Vegetables",   
    "Dairy Products",   
    "Seafood" };  

List<Product> products = GetProductList(); 

var q = 
    from c in categories 
    join p in products on c equals p.Category 
    select new { Category = c, p.ProductName }; 

foreach (var v in q) 
{ 
    Console.WriteLine(v.ProductName + ": " + v.Category);  
} 

more samples in: http://code.msdn.microsoft.com/LINQ-Join-Operators-dabef4e9

这篇关于如何实现实体框架代码首先加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆