Question

now i my code when Name entry is duplicate it counts the duplicate names & add the count number next to the name like

if database has 'Andy' Name already then next Andy will be added as 'Andy 1'

but the problem is if i add another 'Andy' to database it checks for duplicate when its find 'Andy' it becomes 'Andy 1' but again it find 'Andy 1' so at last it get saved as 'Andy 1 1' so i cant figures out how to make it get save by 'Andy 1' then 'Andy 2' then '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);
            }
        }
    }

i am trying to use strings StartsWith in Counting like

string Check = txtName.Text;

txtCOUNT.Text = a.Count (b => b.Name == Check.StartsWith(b.Name)).ToString();

but no its giving me error PLEASE HELP

Was it helpful?

Solution

First thing to note is that when you do :

var a = from b in DB.GetTable<Table>() select b;

foreach (var x in a)
{

It's going to load every row (instead of effecting a db query) which could be costly if there is a lot of row.

Here is a Linq query which i think will do what you want (it will work if the name in txtName.Text contains only text):

using (DBDataContext DB = new DBDataContext(strConnectionString))
{
            string name = txtName.Text.ToString(),
            int nbDuplicate = (from b in DB.GetTable<Table>()
                    where b.Name==name ||  b.Name.StartsWith(name+" ") 
                       && (b.Name.Length>=name.Length+2 && 
                    b.Name[name.Length+1]>='0'
                    && b.Name[name.Length+1]<='9'
                      )
                    select b).Count();
            name +=  " " + nbDuplicate;
              .....
}

OTHER TIPS

Here's your query

var countNumber = (from c in svcContext.ContactSet
                     where c.FirstName.StartsWith("Andy")
                     select new
                     {
                      c.FirstName,
                      c.LastName
                     }).Count();

And since this returns an observeable collectionset then we will obviously call the count to get the no of elements. Count in Linq to Sql is achieved like that only no Linq query for count.

Check Msdn link

Count In Linq

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top