2016-04-29 2 views
-1

Ich habe zwei Tabellen. Die Word Tabelle enthält eine Liste von Wörtern und die WordForm Tabelle enthält eine Liste von Wortarten für jedes Wort.Wie bekomme ich eine "flache" Ausgabe von Daten aus einer Eltern/Kind-Tabelle mit LINQ

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

} 

public class WordForm 
{ 
    public string WordFormId { get; set; } // WordFormId (Primary key) (length: 20) 
    public string WordId { get; set; } // WordId (length: 20) 
    public int PosId { get; set; } // PosId 
    public string Definition { get; set; } // Definition 
    public virtual Pos Pos { get; set; } // FK_WordFormPos 
    public virtual Word Word { get; set; } // FK_WordFormWord 
} 

Ich habe versucht, diese beiden Tabellen wie diese Verbindung:

var word = db.Words 
      .Include(wf => wf.WordForms) 
      .AsNoTracking() 
      .FirstOrDefaultAsync(); 

Das ist mir eine Ausgabe in Objektform gibt.

Wie kann ich zu sehen, was in den Tabellen ist so, dass ich mir Daten wie diese sind zurückgekehrt, wo jede Zeile alle diese Spalten:

WordId CategoryId WordFormId PosId Definition 
WordId CategoryId WordFormId PosId Definition 
WordId CategoryId WordFormId PosId Definition 
WordId CategoryId WordFormId PosId Definition 
+0

Wo Sie Ihre Daten tun müssen letztlich platzieren? – Efrain

Antwort

1

Sie SelectMany verwenden können, um die Ausgabe zu glätten:

var words = db.Words 
       .Include(wf => wf.WordForms) 
       .SelectMany(w => w.WordForms.Select(wf => new 
       { 
        WordId = w.WordId, 
        CategoryId = w.CategoryId, 
        WordFormId = wf.WordFormId, 
        PosId = wf.PosId, 
        Definition = wf.Definition 
       })); 

Wenn Sie das Ergebnis nur für die erste Word wollen:

var word = await db.Words 
        .Include(wf => wf.WordForms) 
        .AsNoTracking() 
        .FirstOrDefaultAsync(); 

var output = word.WordForms.Select(wf => new 
{ 
    WordId = wf.Word.WordId, 
    CategoryId = wf.Word.CategoryId, 
    WordFormId = wf.WordFormId, 
    PosId = wf.PosId, 
    Definition = wf.Definition 
}); 
+0

Danke für deine wirklich gute Antwort. Wenn ich nur eine Kategorie auswählen wollte, würde ich das einfach mit einem .Where (w => w.CategoryId = 123) nach db.Words machen? – Alan2

+0

Ja, das ist der Weg. –

+0

Sorry, ist nach dem 'Include' –

2

Sie können nur die WordForm verwenden, um mit zu beginnen

from wf in db.WordForms 
    //your where conditions even on wf.Word 
    select new 
    { 
     WordId = wf.Word.WordId, 
     CateogoryId = wf.Word.CategoryId, 
     WordFormId = wf.WordFormId, 
     PosId = wf.PosId, 
     Definition = wf.Definition 
    } 
Verwandte Themen