0

I have the following code to join two tables in an ASP.NET MVC controller but I also want to display employee's supervisor name from the same table and same filed.

But I get an error:

An anonymous type cannot have multiple properties with the same name

Here is my code

public IHttpActionResult GetEmployees()
{
    var query = (from n in db.Employees
                 join c in db.tblCities on n.ProjectID equals c.CityID
                 into nc
                 from c in nc.DefaultIfEmpty()

                 join manager in db.Employees on n.ManagerName equals manager.Name
                 into pc
                 from managerin pc.DefaultIfEmpty()

                 select new
                         {
                             n.Name,
                             manager.Name,
                             n.Email,
                             c.CityName
                         });

    var employees = query.ToList();

    return Ok(employees);
}

What is the workaround to accomplish my goal?

1 Answer 1

2

You need to give the properties (or at least conflicting ones) actual names so you know what is what. Which Name would be Name if you don’t? For example:

select new
{
  n.Name
  ManagerName = manager.Name,
  n.Email,
  c.CityName
}

Also often it’s better to use actual types instead of anonymous ones so you get type safety properly through the application, especially when this information is given out to views etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.