简体   繁体   中英

Auto generated id with character i made

string SetTeacherId()
    {
        char digit = 'T';
        string id = "";
        var count = db.Teachers.Count();
        if (count > 0)
        {
            var str = db.Teachers.OrderByDescending(a => a.TeacherID).Select(a => a.TeacherID).First();
            string digits = new string(str.Where(char.IsDigit).ToArray());
            string letters = new string(str.Where(char.IsLetter).ToArray());
            int numbers;
            if (!int.TryParse(digits, out numbers))
            {

            }
            id += letters + (++numbers).ToString("D4");

            return id;
        }
        else
            return digit +"0001";
    }

In here i make a method called SetTeacherId. To be honest it made by my senior. The Teacher Id will auto increment. Can someone make a lot simpler code to auto generate id, because it a little bit confused.

First of all, explaining what your ID datatype in the Table is, would have been nice.

We can only assume you're using a NVARCHAR(x) to represent the IDs.

I'm not sure if you can or would like to make changes to your tables, but you can make the ID column an IDENTITY and a numeric Datatype, and there you can define the seed (starting value) and incremental step (how much bigger the next value will be) and let the DataBAse Management System deal with it. example: DBA Stackexchange topic

But since you're in the C# development, I assume you'd rather use a programmatical approach. What your function does is, creates a simple hash from the letters used in the previous ID and the last number, incremeted by one and padded so it contains 4 digits (Hence the D4 in the ToString function)

Example: Teacher0001 Teacher0002 Teacher0003 ... Teacher9999

To make it simpler, you could have just make the ID into a numeric data type, read the biggest one and just increment by 1 and insert with other values. Usually IDs are numeric and not really descriptive, as they should be used for machines to comprehend them quickly, not humans.

But it's much simpler and cleaner to let the DB deal with it, like described in my 3rd paragraph..

I think the code is quite clear already.

var str = db.Teachers.OrderByDescending(a => a.TeacherID).Select(a => a.TeacherID).First();

This statement get the largest teacher id.

string digits = new string(str.Where(char.IsDigit).ToArray());

This statement get the digit part of the id.

int.TryParse(digits, out numbers)

This statement parse the digit string to int.

id += letters + (++numbers).ToString("D4");

This statement make a new id string by the letter forward and the plus-one digit. The only advice I can give is adding the error handler in if block.

If an explanation could be an answer, this is mine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM