现在我的代码在名称条目重复时计算重复的名称并在名称旁边添加计数
如果数据库已经有“Andy”名称,那么下一个 Andy 将被添加为“Andy 1”
但问题是,如果我在数据库中添加另一个“Andy”,它会在找到“Andy”时检查是否重复,它会变成“Andy 1”,但它再次找到“Andy 1”,所以最后它被保存为“Andy 1 1”所以我不知道如何让它被'Andy 1'然后'Andy 2'然后'Andy 3'保存
private void button0_Click(object sender, EventArgs e)
{
using (DBDataContext DB = new DBDataContext(strConnectionString))
{
Table newTable = new Table
{
Name = txtName.Text.ToString(),
Add = txtAdd.Text.ToString(),
};
var a = from b in DB.GetTable<Table>() select b;
foreach (var x in a)
{
if (x.Name == txtName.Text.ToString())
{
txtCOUNT.Text = a.Count(b => b.Name == txtName.Text).ToString();
MessageBox.Show("This Name is already Exists");
Check = txtName.Text + " " + txtCOUNT.Text;
txtName.Text = Check;
txtAdd.Text = " ";
id = x.Id;
}
}
if (txtName.Text != "" && txtAdd.Text != "")
{
DB.NTable.InsertOnSubmit(newTable);
DB.SubmitChanges();
MessageBox.Show("Name Added Successfully.", "Done!", MessageBoxButton.OK);
NavigationService.GoBack();
}
else
{
MessageBox.Show("Some essential details must be entered. Name, Add.", "Details Missing!", MessageBoxButton.OK);
}
}
}
我正在尝试在计数中使用字符串 StartsWith
string Check = txtName.Text;
txtCOUNT.Text = a.Count (b => b.Name == Check.StartsWith(b.Name)).ToString();
但没有它给我错误请帮助