2016-06-01 6 views
-1

私はサンプル文を記述するためにこのクラスを使用します。List <string>ではなくSampleSentenceを必要とするオブジェクトにList <string>を追加するにはどうすればよいですか?

public class SampleSentence 
{ 
    public int SampleSentenceId { get; set; } // SampleSentenceId (Primary key) 
    public int WordFormId { get; set; } // WordFormId 
    public string Text { get; set; } // Text 
    public int SourceId { get; set; } // SourceId 
    public int StatusId { get; set; } // StatusId 

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

    public SampleSentence() 
    { 
     SourceId = 1; 
     StatusId = 1; 
    } 
} 

とSampleSentences WordFormsの一部です:

public class WordForm 
{ 
    public int WordFormId { get; set; } // WordFormId (Primary key) 
    public int WordId { get; set; } // WordId 
    public string Definition { get; set; } // Definition (length: 200) 
    public int PosId { get; set; } // PosId 
    public int SourceId { get; set; } // SourceId 
    public int StatusId { get; set; } // StatusId 

    // Reverse navigation 
    public virtual System.Collections.Generic.ICollection<SampleSentence> SampleSentences { get; set; } // SampleSentence.FK_SampleSentenceWordForm 
    public virtual System.Collections.Generic.ICollection<Synonym> Synonyms { get; set; } // Synonym.FK_SynonymWordForm 

    // Foreign keys 
    public virtual Pos Pos { get; set; } // FK_WordFormPos 
    public virtual Word Word { get; set; } // FK_WordFormWord 

    public WordForm() 
    { 
     SourceId = 1; 
     StatusId = 1; 
     SampleSentences = new System.Collections.Generic.List<SampleSentence>(); 
     Synonyms = new System.Collections.Generic.List<Synonym>(); 
    } 
} 

私はこのような新しいWordFromを作成する方法を知っている:

var wordForm = new WordForm() 
         { 
          WordId = word.WordId, 
          Definition = result.definition, 
          SampleSentences = sampleSentences 
          PosId = pos 
         }; 

しかし、私のSampleSentencesはこのようにしていることを考える:

List<string> = sampleSentences 

Ho w wordFormにsampleSentencesを追加できますか?

+1

あなたは 'リストを変換する必要があります'を' ICollection 'に追加しましたか?どのように文字列を「文章」に変換すべきかを知っているべきですか?リスト内のこれらの文字列は 'SampleSentenceId'を参照していますか?それでは 'List 'をうまく使うべきでしょうか? – HimBromBeere

答えて

2

を、私は私ができるように、それはのようにシンプルにしてみましょう:

//As sampleSentences are List of strings 
var newListOfSentences = new List<SampleSentence>(); 

foreach (string value in sampleSentences) 
    { 
     var newSentence = new SampleSentence(); 
     newSentence.Text = value ; 
     //and the others... 
     newListOfSentences.Add(newSentence); 
    } 

最後にあなたが持っている:

SampleSentences = newListOfSentences 
+0

ありがとうございます。新しいList ()は、私がやり方が分からなかった大きなものでした。 – Alan2

1

sampleSentencesあなたは簡単にこれを使用してWordFormに実際のインスタンスを置くことができるSampleSentence -instancesだけのIDが含まれているリストを仮定:

var allSentences = // .. get all SampleSentences from your DB or whatever 
myWordForm.SampleSentences = allSentences 
    .Where(x => sampleSentences.Select(y => Convert.ToInt32(y)).Contains(x.SampleSentenceId)); 

とにかくあなたの代わりにList<int>であなたのインスタンスのIDを格納する必要がありますSampleSentenceIdが既にであるため、List<string>の整数はです。

そうでなければ、あなたのsamleSentences -listがあなたのSampleSentence -instancesのText -propertyを参照する場合は、この書くことができます。

myWordForm.SampleSentences = allSentences.Where(x => sampleSentences.Contains(x.Text)); 
関連する問題