several questions of calculation in class model.

sblb 1,231 Reputation points
2022-09-11T16:58:14.19+00:00

Hi ,

I've made a class model to do a calculation to obtain a result : Id/2022, Id is a variable.

Class model :

 public partial class Developer  
    {   
        [Key]  
        public int DeveloperId{ get; set; }  
        public int Id { get; set; }  
        public DateTime DateCrea {get; set;}  
        public string? YearC {   
            get{   
            return DateCrea.Year.ToString();  
            } set{}}  
        public string ECR {  
            get{  
                 string _currentYear = DateTime.Today.Year.ToString();  
                 int id ;  
                 Id = DeveloperId  
                 if (_currentYear == YearC)  
                 {  
                    id = Math.Max(Id,1) + 1;  
                 }  
                else {id =1 ;}  
                string ecr = id.ToString() + '/' + YearC;  
                return ecr;  
            }  
        }         
    }  

the result of the class is e.g : 2/2022

I've four questions :

  • The column ECR is not fill in table but it's rendering in UI. Why the value in column ECR is not write in the table?
  • As you can see the result is 002/2022 which is the calculation of max(Id,1)+1. How can I start at 002/2022?
  • In UI I 've the possibility to delete a line in my table. I can do that with the controller. When I delete the row in table the Id is no longer a series of numbers. This means if there are 5 rows in the table the Id takes the values from 1 to 5;
    If I delete a line for example line 2 the Id takes the values : Id = 1, Id = 3, Id = 4, Id = 5. Why the Id does not increment itself. How to make the Id auto-increment?
  • I would like to add the format 001/2022 i.e. add zeros in front of the Id in the following way: Id takes the values 1 to 9 then the result will be 009/2022 if Id takes the values from 10 to 99 then the result will be 099/2022 ...

Thanks in advance to your help!

Developer technologies | .NET | Blazor
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,596 Reputation points Volunteer Moderator
    2022-09-11T19:17:18.163+00:00

    Perhaps the following method will get what you want in regards to auto-increment. See it in action here

    public class Helpers  
    {  
        public static string NextValue(string sender)  
        {  
            string value = Regex.Match(sender, "[0-9]+$").Value;  
            return sender[..^value.Length] + (long.Parse(value) + 1)  
                .ToString().PadLeft(value.Length, '0');  
        }  
    }  
    

  2. sblb 1,231 Reputation points
    2022-09-12T11:23:01.933+00:00

    I tried your proposition but it doesn't quite do what I want.
    Indeed I have to increment with the max of the Id each time that the condition _currentYear == YearC is verified.

    The code below make the job :

    public string ECR {  
                 get{  
                      string _currentYear = DateTime.Today.Year.ToString();  
                      int id ;  
                      Id = DeveloperId  
                      if (_currentYear == YearC)  
                      {  
                         id = Math.Max(Id,1) + 1;  
                      }  
                     else {id =1 ;}  
                     string ecr = id.ToString() + '/' + YearC;  
                     return ecr;  
                 }  
             }         
    

    The result is eg :
    240057-image.png

    I have always the question :

    • The column ECR is not fill in table but it's rendering in UI. Why the value in column ECR is not write in the table?
    • As you can see the result is 002/2022 which is the calculation of max(Id,1)+1. How can I start at 002/2022?
    • I would like to add the format 001/2022 i.e. add zeros in front of the Id in the following way: Id takes the values 1 to 9 then the result will be 009/2022 if Id takes the values from 10 to 99 then the result will be 099/2022 ...

    Thanks in advance to your help!!


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.