1

我是 C# 新手。我正在大学的一个模块中学习它。我们被分配了一项任务,其中涉及我们必须使用 Visual Studio 工具箱中包含的各种组件创建一个简单的预订应用程序。

UI 具有一个ListBox使用户能够选择将参加该活动的人员的多个姓名。当用户确认选择时,所选项目将连接到 aString并在 a 中输出。Label

这是我从中获取值的代码ListBox

 protected void btnRequest_Click(object sender, EventArgs e)
{
    //Update the summary label with the details of the booking.
    n = name.Text;
    en = eventName.Text;
    r = room.SelectedItem.ToString();
    d = cal.SelectedDate.ToShortDateString();

    foreach (ListItem li in attendees.Items)
    {
        if (li.Selected)
        {
            people += li.Text + " ";
        }
    }


    confirmation.Text = r + " has been booked on " + d + " by " + n + " for " + en + ". " + people + " will be attending.";
}

以下是我的完整代码:

public partial class _Default : System.Web.UI.Page
{
    //Variables
    public TextBox name;
    public TextBox eventName;
    public Label confirmation;
    public DropDownList room;
    public Calendar cal;
    public Button btn;
    public ListBox attendees;

    //Booking variables - store all information relating to booking in these variables
    public String n; //name of person placing booking
    public String en; //name of event
    public String r; //room it will take place
    public List<String> att; //list of people attending
    public String d; //date it will be held on

    public String people;

    protected void Page_Load(object sender, EventArgs e)
    {
        //Get references to components
        name = txtName;
        eventName = txtEvent;
        room = droplistRooms;
        attendees = attendeelist;
        cal = Calendar1;
        btn = btnRequest;
        confirmation = lblSummary;

    }
    protected void btnRequest_Click(object sender, EventArgs e)
    {
        //Update the summary label with the details of the booking.
        n = name.Text;
        en = eventName.Text;
        r = room.SelectedItem.ToString();
        d = cal.SelectedDate.ToShortDateString();

        foreach (ListItem li in attendees.Items)
        {
            if (li.Selected)
            {
                people += li.Text + " ";
            }
        }


        confirmation.Text = r + " has been booked on " + d + " by " + n + " for " + en + ". " + people + " will be attending.";
    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        d = cal.SelectedDate.ToShortDateString();
    }

输出是这样的:

Jason Manford 已于 2013 年 8 月 10 日为喜剧演出预订了房间 2。Jack Coldrick Bill Gates Larry Page Jimmy Wales 将出席。

但是,我想在参加活动的人的姓氏中添加一个和。我将如何去做这件事。我必须使用一个List吗?

非常感谢...

4

5 回答 5

3

尝试将所选项目复制到另一个集合中并使用一个简单的计数器:

int counter = 1; // start at 1 so that the counter is in line with the number of items that the loop has iterated over (instead of 0 which would be better for indexing into the collection)

List<ListItem> selectedItems = new List<ListItem>();

foreach (ListItem li in attendees.Items)
{
    if (li.Selected)
    {
        selectedItems.Add(li);
    }
}

foreach (ListItem li in selectedItems)
{
    counter++;

    if (selectedItems.Count > 1 && i == selectedItems.Count) // check after the counter has been incremented so that only the last item triggers it
    {
        people += " and";
    }

    people += li.Text + " ";
}

正如一些人指出的那样,您还应该考虑使用 a StringBuilder,因为字符串在 .Net 中是不可变的,这意味着它们不能被修改。每次将文本附加到字符串时,都会在幕后使用新内容创建一个新字符串,并丢弃旧字符串。可以想象,如果列表中有很多名称,最终可能会影响性能。下面的例子:

List<ListItem> selectedItems = new List<ListItem>();

foreach (ListItem li in attendees.Items)
{
    if (li.Selected)
    {
        selectedItems.Add(li);
    }
}

StringBuilder sbPeople = new StringBuilder();
int counter = 1; // start at 1 so that the counter is in line with the number of items that the loop has iterated over (instead of 0 which would be better for indexing into the collection)

foreach (ListItem li in attendees.SelectedItems)
{
    counter++;

    if (selectedItems.Count > 1 && i == selectedItems.Count) // check after the counter has been incremented so that only the last item triggers it
    {
        sbPeople.Append(" and");
    }

    sbPeople.Append(li.Text);
    sbPeople.Append(" ");
}

参考StringBuilder文档:http: //msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx

于 2013-10-02T14:21:37.723 回答
3
var p = new List<string>() {"person1", "person2", "person3"};
string people;
if(p.Count == 1)
    people = p[0];
else
    people = string.Join(", ", p.Take(p.Count - 1)) + " and " + p[p.Count - 1]

为了更好地适应你的代码,我会写这样的东西(正如评论中间接建议的那样):

var p = attendees.Items.OfType<ListItem>.Where(y => y.Selected).Select(y => y.Text).ToList();
var people = "";
if(p.Count == 1)
    people = p[0];
if(p.Count > 1)
    people = string.Join(", ", p.Take(p.Count - 1)) + " and " + p[p.Count - 1]

Confirmation.Text = string.Format("{0} has been booked on {1} by {2} for {3}. {4} will be attending", r, d, n, en, people);
于 2013-10-02T14:23:29.323 回答
2

你可以用这个替换你的 foreach

var peopleList = new List<string>();

foreach (ListItem li in attendees.Items)
{
    if (li.Selected)
       peopleList.Add(li.Text);
}

var people = string.Join(",", list.Take(list.Count - 1));
people += list.Count > 1 ? " and " + list.Last() : list.Last();
于 2013-10-02T14:25:48.320 回答
0

为了使答案与您的问题保持一致,我将使其易于理解-尽管我使用 StringBuilder 而不是附加字符串-我已经看到此方法可以更快地处理更大的字符串。

var selectedPeople = new List<string>();

foreach (ListItem li in attendees.Items)
{

    if (li.Selected)
    {
        people.Add(li.Text);
    }

}
var sb = new StringBuilder();
if (selectedPeople.Count == 0)
{
    sb.Append("");
}
else
{
    for (var i = 0; i < selectedPeople.Count; i++)
    {
        if (i > 0)
        {
            sb.Append(" ");
            if (i == selectedPeople.Count)
            {
                sb.Append("and ");
            }
        }
        sb.Append(selectedPeople[i]);
    }
}


...

confirmation.Text = r + " has been booked on " + d + " by " + n + " for " + en + ". " + sb.ToString() + " will be attending.";

我建议您查看另一个建议使用 SelectedItems 的答案,以防止必须执行第一个循环。

于 2013-10-02T14:33:21.130 回答
0
var selectedPeople = list.Items.OfType<ListItem>().Where(c => c.Selected).Select(c => c.Text).ToList();
string people;
if (selectedPeople.Count == 1)
    people = selectedPeople[0];
else
    people = string.Join(", ", selectedPeople.Take(selectedPeople.Count - 1)) 
                + " and " + selectedPeople[selectedPeople.Count - 1];
于 2013-10-02T14:37:54.913 回答