Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Problem
How to get max value based on composite key branchCode,EmployeeId
i work on asp.net core 2.1 visual studio 2017
How to get max number per branch code not for all branches ?

repository interface

public interface IrepositoryTab<T> where T : class
        {
            IEnumerable<T> GetAll();
        }


repository implementaion

public class RepositoryTab<T> : IrepositoryTab<T> where T : class
    {
        protected TabDbContext db { get; set; }
        private DbSet<T> dbSet;

        public RepositoryTab(TabDbContext Tabdb)
        {
            db = Tabdb;
            dbSet = db.Set<T>();
        }
        public IEnumerable<T> GetAll()
        {
            return dbSet.ToList();
        }
        }

public class Employee
    {
  
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int EmployeeId { get; set; }
        public int BranchCode { get; set; }
        public string EmployeeName { get; set; }
        public int EmployeeAge { get; set; }

    }

modelBuilder.Entity<Employee>()
          .HasKey(t => new { t.EmployeeId, t.BranchCode });


What I have tried:

public class EmployeeController : Controller
   {

       private readonly IrepositoryTab<Employee> _repository;
       public EmployeeController(IrepositoryTab<Employee> emp)
       {
           this._repository = emp;
       }

       public async Task<IActionResult> Create(int? id)
       {
           var model = new Employee();
           if (id == null)
           {

               model.EmployeeId = _repository.GetAll().Max(Employee => Employee.EmployeeId);


           }
           }
Posted
Updated 25-Jan-19 12:17pm
v3
Comments
Dave Kreskowiak 25-Jan-19 19:08pm    
Why would you want to? In the situation I see in your code, you would just create the employee record with all appropriate fields and then get the database generated ID number back.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900