2016-05-16 8 views
2

私はこのクラスを持っている:LINQクエリで複数の子テーブルを含めるにはどうすればよいですか?

public class Word 
{ 
    public string WordId { get; set; } // WordId (Primary key) (length: 20) 
    public int WordIdentity { get; set; } // WordIdentity 
    public virtual System.Collections.Generic.ICollection<WordForm> WordForms { get; set; } // WordForm.FK_WordFormWord 
    public Word() 
    { 
     WordForms = new System.Collections.Generic.List<WordForm>(); 
    } 
} 

public class WordForm { 
    public string WordFormId { get; set; } // WordFormId (Primary key) (length: 20) 
    public int WordFormIdentity { get; set; } // WordFormIdentity 
    public string WordId { get; set; } // WordId (length: 20) 
    public virtual System.Collections.Generic.ICollection<SampleSentence> SampleSentences { get; set; } // SampleSentence.FK_SampleSentenceWordForm 
    public virtual System.Collections.Generic.ICollection<WordDefinition> WordDefinitions { get; set; } // WordDefinition.FK_WordDefinitionWordForm 
    public WordForm() 
    { 
     SampleSentences = new System.Collections.Generic.List<SampleSentence>(); 
     WordDefinitions = new System.Collections.Generic.List<WordDefinition>(); 
    } 
} 

public class SampleSentence 
{ 
    public int SampleSentenceId { get; set; } // SampleSentenceId (Primary key) 
    public string WordFormId { get; set; } // WordFormId (length: 20) 
    public string Text { get; set; } // Text 

    // Foreign keys 
    public virtual WordForm WordForm { get; set; } // FK_SampleSentenceWordForm 
} 

public class WordDefinition 
{ 
    public int WordDefinitionId { get; set; } // WordDefinitionId (Primary key) 
    public string WordFormId { get; set; } // WordFormId (length: 20) 
    public string Text { get; set; } // Text (length: 50) 
    public int? Ascii { get; set; } // Ascii 

    // Foreign keys 
    public virtual WordForm WordForm { get; set; } // FK_WordDefinitionWordForm 
} 

私は、単語のためのデータを取得しようとwordForm、SampleSentencesとWordDefinitionsが、私はないですよ選択をコード化する方法を確認してください。ここで私がこれまで持っているものです。

var words = db.Words 
      .Include(w => w.WordForms) 
      // How do I include SampleSentences 
      // and WordDefinitions ? 
      .AsNoTracking() 
      .ToListAsync(); 

は、誰かが私もSampleSentencesとWordDefinitionsを含めることができる方法を教えてもらえますか?私はこれを実行しようとしましたが、それは構文チェックを失敗しています:

.Include(w => w.WordForms.SampleSentences) 

答えて

3

をちょうどあなた含めるに選択を使用します。

.Include(w => w.WordForms.Select(f => f.SampleSentences)) 
.Include(w => w.WordForms.Select(f => f.WordDefinitions)) 
+0

ありがとうございました。私がこれを行うなら、私はまだ平易にする必要があります:。インクルード(w => w.WordForms)も同様ですか? –

+1

@SamanthaJ no、含まれるパスにはすでにすべてのサブパスが含まれているためです。 – Evk

関連する問題