2017-03-03 13 views
0

私は2つのEFのモデルがあります。たとえば、MedicamentMedicamentsコンテンツの場合LINQクエリ

public class Medicament 
{ 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public Guid Id { get; set; } 

    [Required] 
    public string InternationalName { get; set; } 

    public ICollection<MedicamentMedicaments> MedicamentMedicamentses { get; set; } 
} 

public class MedicamentMedicaments 
{ 
    [Key] 
    [Column(Order = 0)] 
    public Guid MedicamentId { get; set; } 

    [Key] 
    [Column(Order = 1)] 
    public Guid AnalogId { get; set; } 

    public Medicament Medicament { get; set; } 
    public Medicament Analog { get; set; } 
} 

  • を|メディケアマート| AnalogId |
  • | 1 | 5 |
  • | 5 | 3 |

医薬品類似物質をLINQクエリで受信したいのですか?

答えて

0

私は解決策が見つかりました:アナログMedicamentMedicamentsにに保存することができますので、それは、私の場合、仕事をdoesnの `t

var query = from medicament in Db.Medicaments 
      where medicament.Id == medId 
      from mm in Db.MedicamentMedicamentses 
      where medicament.Id == mm.MedicamentId || medicament.Id == mm.AnalogId 
      select medicament.Id == mm.MedicamentId ? mm.Analog : mm.Medicament; 
0
List<Analog> result = yourDbContext.MedicamentMedicaments.Where(t => t.MedicamentId == yourMedicamentId).Select(t => t.Analog).ToList() 

または

List<Analog> result = yourDbContext.Medicament.SelectMany(t => t.MedicamentMedicamentses).Where(t => t.MedicamentId == yourMedicamentId).Select(t => t.Analog).ToList() 

または

List<Analog> result = yourDbContext.Medicament.Where(t => t.Id == yourMedicamentId).SelectMany(t => t.MedicamentMedicamentses).Select(t => t.Analog).ToList() 
+0

をMedicamentIdまたはAnalogIdカラム –