简体   繁体   English

如何使用.net Web API从员工列表中获取最新的员工ID(字符串)

[英]How can I get latest employee ID (string) from the list of employees using .net web api Get call

I am using Entity Framework My EmployeeDto class is : 我正在使用Entity Framework我的EmployeeDto类是:

public class EmployeeDto
    {
        [DataMember]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int SerialNumber { get; set; }
        [DataMember]
        [Key]
        [Required]
        public string ID { get; set; }
        [DataMember]
        [Required]
        public string FirstName { get; set; }
        [DataMember]
        public string MiddleName { get; set; }
        [DataMember]
        [Required]
        public string LastName { get; set; }
}

My Dto class is EmployeeDto and Dal class is named as Employee. 我的Dto类是EmployeeDto,Dal类被命名为Employee。 I want to get the maximum value of EmployeeID from the database and provide it to frontend through Get call . 我想从数据库中获取EmployeeID的最大值,并通过Get call将其提供给前端。

My Get call to get the list of all employees is : 我的“获取”以获取所有员工的列表的电话是:

public List<EmployeeDto> GetAllEmployees()
        {
            var employeeDto = new List<EmployeeDto>();
            using (EmployeeDataEntities entities = new EmployeeDataEntities())
            {
                var employeeData = entities.Employees.ToList().Where(e => e.IsActive == true);

                List<Employee> emp = employeeData.ToList(); 
                //emp.FindLastIndex(e => e.)
                employeeDto = Mapper.Map<List<Employee>,List<EmployeeDto>>(emp);
            };
            return employeeDto;
        }

This is my GetLatestEmployeeByID code : 这是我的GetLatestEmployeeByID代码:

public int GetEmployeeLatestID(EmployeeDto employeeDto)
        {
            using (EmployeeDataEntities entities = new EmployeeDataEntities())
            {
                var employeeData = entities.Employees.ToList().Where(e => e.IsActive == true);
                List<Employee> emp = employeeData.ToList();
                emp.FindLastIndex(e => e.ID);                              
            }
        }

I have tried a couple of solutions and i end up with this one: 我尝试了几种解决方案,最终得到了以下解决方案:

var a = new List<EmployeeDto>()
        {
            new EmployeeDto()
            {
                SerialNumber = 1,
                ID = "AB01",
                FirstName = "Ala",
                MiddleName = "b",
                LastName = "ala"
            },
            new EmployeeDto()
            {
                  SerialNumber = 2,
                ID = "AB02",
                FirstName = "Ala",
                MiddleName = "b",
                LastName = "ala"
            },new EmployeeDto()
            {
                  SerialNumber = 3,
                ID = "AB03",
                FirstName = "Ala",
                MiddleName = "b",
                LastName = "ala"
            }
        };

        // biggestIdAsInt = 230 a.max returns the max value after the calculations 
        var biggestIdAsInt = a.Max(employee => Encoding.ASCII.GetBytes(employee.ID) // get Employee Id as byte array
        .Sum(b => b)); // summ the bytes for each employee 

        // 230


var substractedNumberFromBiggestId = Regex.Match(a.FirstOrDefault(x => x.ID.ToCharArray().Sum(y => y) == biggestIdAsInt).ID, @"\d+").Value;
        Console.WriteLine(substractedNumberFromBiggestId);

I have put comments to make the code a little bit clearer. 我已经添加了注释以使代码更加清晰。

暂无
暂无

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

相关问题 如何在 ASP.NET Core Web API 中使用 LINQ 仅从数据库获取最新 ID? - How to get latest id only from database using LINQ in ASP.NET Core Web API? ASP.NET 核心 Web API - 如何在实体框架结果中获取所有员工用户详细信息和角色 - ASP.NET Core Web API - How get all employees user details and roles in Entity Framework result 如何使用令牌授权和参数从 .NET 客户端调用 Web API GET 方法 - How to call a Web API GET method from a .NET client using also token authorization and parameters 创建班级Employee并使用SELECT将所有雇员带到GridView - Create class Employee and Get all employees to a GridView using SELECT Bot Framework.Net,如何从我的 Web 应用程序中获取用户名、ID、令牌到 Bot - Bot Framework .Net, How I can get either the Username, Id, Token from my Web App to Bot 如何在 ASP.NET Core 6 Web API 中以像素形式获取图像内容并从中创建图像? - How can I get image content as pixel and create image from it in ASP.NET Core 6 Web API? 如何从包含 asp.net 中确定字符串值的字符串列表中获取项目? - How can I get the items from a list of string that contain a determinate string value in asp.net? 如何从Web API调用获得响应 - How to get response from web API call 如何使用api在Facebook上获取任何照片的ID - How can i get id of any photo on Facebook using api 如何使用 LINQ 递归获取员工列表及其报告级别 - How to get the list of employees and their reporting level recursively using LINQ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM