在ForEach循环下时,StringBuilder无法正常工作

时间:2019-01-02 10:29:05

标签: c# stringbuilder

这里我有一些名字,但名字没有绑定到String Builder

var x=from n in d.Employee
 Where n.EmpId==10
select n

 foreach (var master in x)
            {
                StringBuilder sb = new StringBuilder();
                StringBuilder abc = new StringBuilder();
                sb.Append(master.CANDIDATE_NAME + ",";
                abc.Append(sb);
                join_Body = new HrEmailsender()
                {
     Body = "Hi," + abc +                   
                };

请给我拨款人提示以解决此问题

2 个答案:

答案 0 :(得分:1)

您要在每个循环中创建一个新的stringbuilder(sb,abc),但是您只想在每个循环中添加一个要添加一些东西的stringbuilder,请在循环外部创建stringbuilder:

var x=from n in d.Employee
     Where n.EmpId==10
    select n

                    StringBuilder sb = new StringBuilder();
                    StringBuilder abc = new StringBuilder();

     foreach (var master in x)
                {

                    sb.Append(master.CANDIDATE_NAME + ",");
                    abc.Append(sb);
                    join_Body = new HrEmailsender()
                    {
         Body = "Hi," + abc +                   
                    };

答案 1 :(得分:1)

您将必须在每个循环的外部声明字符串生成器。 消息正文也应在foreach循环之后生成,以便包括所有名称。

let defaultAction = UIAlertAction(title: "Melden", style: .default) { _ in
    if let text = alert.textFields?.first?.text, !text.isEmpty {
        let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
        ref.setValue(["postId": postId, "reason": text])
    }
}