I have the following two names in my database: SMITH and SMITH1
If the name comes out in the database, I want to either add 1 to it, if only SMITH quits, and if the name with the number is already completed, I want to increase the digital part of the name.
Is it possible to do this in the same LINQ status? Right now, I am doing:
string name = string.Format("{0}{1}{2}",first_name,middle_name,last_name).ToUpper(); //Check for duplicates while(names.SingleOrDefault (d => d.Name== name) != null) { //Get last char char lastChar = name[name.Length - 1]; //Last char is number if(Char.IsNumber(lastChar)) { name = name.Replace(lastChar,Convert.ToChar(Convert.ToInt32(lastChar) + 1)); } else { name = (name + 1); } }
Since I have a LINQ query in my while loop, will it select every time it has to loop back?
Also, if anyone knows a way to make it more concise but readable, would that be great?
This works for names that either don't have a number, or 1 to 9, but what happens when I get to 10 it only replaces the last character, in this case 0, how do I do it?
Is there any way to get the numeric part in LINQ, so SMITH12 will return 12
source share