简体   繁体   中英

Joining two tables in asp.net webapi 2

Need help joining two table in asp.net webapi 2

I have the following code to join two table

  public IQueryable<Employee> GetEmployees()
    {


        var q = (from n in db.Employees
                 join c in db.tblCities on n.ProjectID equals c.CityID

                 select new
                 {
                     n.Name,
                     n.Email,
                     c.CityName,

                 }).ToList();
        return q;

    }

but i get this error

Severity Code Description Project File Line Suppression State Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?) WebApplication15 C:\Users\admin\source\repos\WebApplication15\WebApplication15\Controllers\EmployeesController.cs 35 Active

employee model

namespace WebApplication15
{
    using System;
    using System.Collections.Generic;

    public partial class DtscEmployee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Country { get; set; }
        public Nullable<int> ProjectID { get; set; }
        public string ManagerName { get; set; }
        public string ProfileImage { get; set; }

    }
}

city model

namespace WebApplication15
{
    using System;
    using System.Collections.Generic;

    public partial class tblCity
    {
        public int CityID { get; set; }
        public string CityName { get; set; }
    }
}

From the looks of it, you would need to create an Employee class that will contain your required data that you need.

Employee class:

public class Employee 
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string CityName { get; set; }
}

And since you are converting to a List , your method should return a List , so your method would look like:

public List<Employee> GetEmployees()
{
    List<Employee> q =  (from n in db.Employees
                        join c in db.tblCities on n.ProjectID equals c.CityID

                         select new Employee 
                         {
                          Name =n.Name,
                          Email =n.Email,
                          CityName =c.CityName
                         }).ToList();

                   return q;
}

EDIT: Assuming that you have your project setup as a WEB-API, you can design your API by first setting up a Controller method that will return your data:

public class EmployeeController : Controller
{
 [ProducesResponseType(typeof(Employee), 200)]
 [HttpGet("getEmployeeInfo")]
 public ActionResult GetEmployeeInformation()
 {

   var result = GetEmployees(); //Call your method wherever you have defined it

   return result == null ? (IActionResult)NoContent() : Ok(result);
  }

}

To access your API, your path will look like: localhost/api/getEmployeeInfo

let's take this one step at a time starting with your initial code.

IQueryable is an expression that you are building, a query if you want. That's an important distinction, it's not actual data. You can chain multiple conditions on it, this is why returning an IQueryable is sometimes good as it allows this chaining to happen.

When you are ready to execute the expression and get the actual data, this is when you enumerate, so your ToList() at the end will go and run the query, return the data in the format that you expect, List, not IQueryable

Of course, a clear message at this point becomes that your API endpoint should never return IQueryable as that is not data yet.

So, your code would become something like:

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

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

       var employees = query.ToList();

       //some other things, maybe you want to return BadRequest if there are none or NotFound or whatever you deem appropriate
       return Ok(employees);           
    }

a good reading on this is here: https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/action-results

if I were you, I would start simple, get the demo controller in, make sure you can hit the default actions, then modify it with your own ones, take care of the routes, make sure those work properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM