Home > Software design >  Cannot implicitely convert type WebApplication1.models.Table1 to int
Cannot implicitely convert type WebApplication1.models.Table1 to int

Time:09-23

I want to use sql query in a get api as:

[Route("empLevel/{id}")]
public IActionResult GetEmpHierarchy(int id)
{
    List<Employee> emp = entity.Employees.ToList();
    List<Employee> mngr = entity.Employees.ToList();

    var query = (from e in emp
                 join m in mngr on e.MngId equals m.Id into tab1
                 from mngrN in tab1.DefaultIfEmpty()
                 select new Employee { Id = e, MngId = m}).ToList();
    return Ok(query);
}

But I am getting an error on the line ID = e where it is saying that e cannot be converted to int.

In my models class I have:

public partial class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double Grade { get; set; }
    public int? MngId { get; set; }
    public virtual SalarySplit GradeNavigation { get; set; }        
}

Please give a solution.

CodePudding user response:

Variable e represents an object of type Employee. You are trying to assign whole object into the property Id. Instead you should use the value in property of that e object.

var query = (from e in emp
             join m in mngr on e.MngId equals m.Id into tab1
             from mngrN in tab1.DefaultIfEmpty()
             select new Employee { Id = e.Id, MngId = m.Id}).ToList();

Bonus: If I'm not mistaken, the filtering won't be applied there and you get either object with or without manager (both). In that case the query can be simplified:

var query = (from e in emp
             select new Employee { Id = e.Id, MngId = e.MngId}).ToList();

CodePudding user response:

Because e is a sort of an object and your ID is an int. so you probably want something like this.

select new Employee { Id = e.MngId, MngId =  m.Id}).ToList();
  • Related