1

私は次のクラスを持っています:

public class Question {
    public Question() { this.Answers = new List<Answer>(); }
    public int QuestionId { get; set; }
    public string Text { get; set; }
    public virtual ICollection<Answer> Answers { get; set; }
}

public class Answer {
    public int AnswerId { get; set; }
    public string Text { get; set; }
}

文字列をチェックしてエンディングを削除する方法として、誰かが提案した次のものがあります。これをメソッドに入れると、次のようになります。

    public static string cleanQuestion(string text)
    {
        if (text == null) { return null; }
        else {
            return (Regex.Replace(text, "<p>&nbsp;</p>$", ""));
        }
    } 

質問の Text フィールドでこのメソッドを呼び出す方法を知っています。しかし、各 Answer Text フィールドでメソッドを呼び出すにはどうすればよいでしょうか?

4

5 に答える 5

0

また、拡張メソッドのアプローチの方が便利です。

public class Question
{
    public Question() { this.Answers = new List<Answer>(); }
    public int QuestionId { get; set; }
    public string Text { get; set; }
    public virtual ICollection<Answer> Answers { get; set; }
}

public static class TextExtension
{
    public static string CleanQuestion(this Question @this)
    {
        if (@this.Text == null) { return null; }
        else
        {
            return (Regex.Replace(@this.Text, "<p>&nbsp;</p>$", ""));
        }
    } 
}


    // Usage:
    Question q1= new Question();
    q1.CleanQuestion();
于 2013-10-14T12:26:32.637 に答える
0

私はちょうどそれぞれを通過すると思いますQuestion

foreach(Anser answer in question.Answers)
  answer.Text = cleanQuestion(answer.Text);

ただし、としてクラスに追加cleanQuestion()する必要があるかもしれません。このように、 を呼び出すだけで、各回答を個別に処理できます。QuestionmethodCleanQuestions()

于 2013-10-14T12:09:05.500 に答える
0

次のことを試しましたか:

    Question q = new Question();

    foreach (Answer ans in q.Answers) {
        string cleanedText = cleanQuestion(ans.Text);
    }

これは、質問オブジェクト内のコレクション内の各回答を繰り返し処理し、回答テキストのパラメーターを使用してメソッドを呼び出します。

于 2013-10-14T12:06:41.567 に答える