C# 如何更改作为对象一部分的集合中字段的值

C# 如何更改作为对象一部分的集合中字段的值,c#,collections,C#,Collections,我有以下课程: 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; } } publ

我有以下课程:

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; }
}
公开课问题{
公共问题(){this.Answers=new List();}
public int QuestionId{get;set;}
公共字符串文本{get;set;}
公共虚拟ICollection答案{get;set;}
}
公开课答案{
public int AnswerId{get;set;}
公共字符串文本{get;set;}
}
有人建议我使用下面的方法来检查字符串并删除结尾。我把它放到一个方法中,现在看起来是这样的:

    public static string cleanQuestion(string text)
    {
        if (text == null) { return null; }
        else {
            return (Regex.Replace(text, "<p>&nbsp;</p>$", ""));
        }
    } 
公共静态字符串清理问题(字符串文本)
{
如果(text==null){return null;}
否则{
return(Regex.Replace(文本“

$”,”); } }
我知道如何在问题的文本字段中调用此方法。但是我怎么能调用这个方法呢
每个答案文本字段?

将属性更改为字段支持的属性如何:

public class Answer {
    private string _text;

    public int AnswerId { get; set; }
    public string Text
    {
        get { return _text; }
        set { _text = Class.cleanQuestion(value); }
    }
}
其中
Class
static
方法启用的类

现在,每当一个答案得到
文本
,它就会被清除

另一方面。如果希望
文本
包含未清除的值,除非在某些情况下,您可以在
答案
类上构建
get
属性:

public string CleanText
{
    get { return Class.cleanQuestion(this.Text); }
}

您是否尝试过以下方法:

    Question q = new Question();

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

这将在您的问题对象的集合中对每个答案进行评分,并使用答案文本的参数调用该方法。

我认为只需检查每个
问题

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

但是,我还要说,也许您应该将
cleanQuestion()
作为
方法添加到
Question
类中。这样,您只需调用
CleanQuestions()
,它就会自己检查每个答案。

如果您想保留原始答案:

问题类别:

public List<Answer> CleanAnswers {
    get {
        return Answers.Select(a => a.CleanText()).ToList();
    }
}
公共列表{
得到{
返回答案。选择(a=>a.CleanText()).ToList();
}
}
答案类别:

public string CleanText()
{
    if (this.Text == null) { return null; }
    else {
        return (Regex.Replace(this.Text, "<p>&nbsp;</p>$", ""));
    }
} 
公共字符串CleanText()
{
如果(this.Text==null){return null;}
否则{
return(Regex.Replace(this.Text,“

$”,”); } }
您也可以使用扩展方法,这种方法会更方便

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();
公开课问题
{
公共问题(){this.Answers=new List();}
public int QuestionId{get;set;}
公共字符串文本{get;set;}
公共虚拟ICollection答案{get;set;}
}
公共静态类文本扩展
{
公共静态字符串CleanQuestion(this Question@this)
{
如果(@this.Text==null){return null;}
其他的
{
返回(Regex.Replace(@this.Text,“

$”,”); } } } //用法: 问题q1=新问题(); q1.问题();
你试过什么了吗?我本来想用这种方法,但后来认为我只会更改答案的本地副本?这真的会改变问题对象中的实际值吗?它会改变
问题
中的
文本
的值。试试看!