简体   繁体   English

在 asp.net webapi 2 中加入两个表

[英]Joining two tables in asp.net webapi 2

Need help joining two table in asp.net webapi 2需要帮助在 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'.严重性代码 描述 项目文件行抑制 State 错误 CS0266 无法将类型 'System.Collections.Generic.List<>' 隐式转换为 'System.Z7FB58B61118DFE3058EZIBQuery'650842CA An explicit conversion exists (are you missing a cast?) WebApplication15 C:\Users\admin\source\repos\WebApplication15\WebApplication15\Controllers\EmployeesController.cs 35 Active存在显式转换(您是否缺少演员表?) WebApplication15 C:\Users\admin\source\repos\WebApplication15\WebApplication15\Controllers\EmployeesController.cs 35 Active

employee model员工 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城市 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。

Employee class:员工 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:并且由于您要转换为List ,因此您的方法应返回List ,因此您的方法如下所示:

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:编辑:假设您将项目设置为 WEB-API,您可以通过首先设置Controller方法来设计 API,该方法将返回您的数据:

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要访问您的 API,您的路径将如下所示: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. IQueryable 是您正在构建的表达式,如果需要,可以进行查询。 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.您可以在其上链接多个条件,这就是为什么返回 IQueryable 有时很好,因为它允许这种链接发生。

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当您准备好执行表达式并获取实际数据时,这是您枚举的时候,所以最后的 ToList() 将 go 并运行查询,以您期望的格式返回数据,List,而不是 IQueryable

Of course, a clear message at this point becomes that your API endpoint should never return IQueryable as that is not data yet.当然,此时有一个明确的消息是,您的 API 端点不应该返回 IQueryable,因为它还不是数据。

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一个很好的阅读在这里: 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.如果我是你,我会从简单的开始,获取演示 controller,确保你可以点击默认操作,然后用你自己的修改它,注意路线,确保它们正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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