Click here to Skip to main content
14,037,160 members
Rate this:
 
Please Sign up or sign in to 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190424.1 | Last Updated 25 Jan 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100