简体   繁体   English

自动生成的ID与我制作的字符

[英]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. 在这里,我创建了一个名为SetTeacherId的方法。 To be honest it made by my senior. 老实说,这是我上司做的。 The Teacher Id will auto increment. 教师ID将自动递增。 Can someone make a lot simpler code to auto generate id, because it a little bit confused. 有人可以制作出更简单的代码来自动生成id的原因,因为它有点混乱。

First of all, explaining what your ID datatype in the Table is, would have been nice. 首先,解释一下表中的ID数据类型会很不错。

We can only assume you're using a NVARCHAR(x) to represent the IDs. 我们只能假设您使用NVARCHAR(x)表示ID。

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. 我不确定是否可以更改表,但可以将ID列设置为IDENTITY和数字数据类型,然后在其中定义种子(起始值)和增量步长(更大)下一个值将是),然后让DataBAse管理系统处理它。 example: DBA Stackexchange topic 示例: DBA Stackexchange主题

But since you're in the C# development, I assume you'd rather use a programmatical approach. 但是由于您从事的是C#开发,因此我假设您宁愿使用编程方法。 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) 函数的作用是,根据前一个ID和最后一个数字中使用的字母创建一个简单的哈希,将其加1并加以填充,使其包含4位数字(因此ToString函数中的D4

Example: Teacher0001 Teacher0002 Teacher0003 ... Teacher9999 例如: 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. 为了简化起见,您可以将ID设置为数字数据类型,读取最大的ID,然后递增1,然后插入其他值。 Usually IDs are numeric and not really descriptive, as they should be used for machines to comprehend them quickly, not humans. 通常,ID是数字的,并不是真正的描述性的,因为ID应该用于机器(而不是人类)来快速理解它们。

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. 此语句获得最大的教师ID。

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

This statement get the digit part of the id. 该语句获取id的数字部分。

int.TryParse(digits, out numbers)

This statement parse the digit string to int. 该语句将数字字符串解析为int。

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

This statement make a new id string by the letter forward and the plus-one digit. 该语句通过字母前加加一个数字来创建新的id字符串。 The only advice I can give is adding the error handler in if block. 我能给出的唯一建议是在if块中添加错误处理程序。

If an explanation could be an answer, this is mine. 如果一个解释可能是答案,那是我的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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