簡體   English   中英

在 asp.net webapi 2 中加入兩個表

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

需要幫助在 asp.net webapi 2 中加入兩個表

我有以下代碼來加入兩個表

  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;

    }

但我得到這個錯誤

嚴重性代碼 描述 項目文件行抑制 State 錯誤 CS0266 無法將類型 'System.Collections.Generic.List<>' 隱式轉換為 'System.Z7FB58B61118DFE3058EZIBQuery'650842CA 存在顯式轉換(您是否缺少演員表?) WebApplication15 C:\Users\admin\source\repos\WebApplication15\WebApplication15\Controllers\EmployeesController.cs 35 Active

員工 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; }

    }
}

城市 model

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

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

從外觀上看,您需要創建一個包含所需數據的Employee class。

員工 class:

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

並且由於您要轉換為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;
}

編輯:假設您將項目設置為 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);
  }

}

要訪問您的 API,您的路徑將如下所示:localhost/api/getEmployeeInfo

讓我們從您的初始代碼開始,一步一步來。

IQueryable 是您正在構建的表達式,如果需要,可以進行查詢。 這是一個重要的區別,它不是實際數據。 您可以在其上鏈接多個條件,這就是為什么返回 IQueryable 有時很好,因為它允許這種鏈接發生。

當您准備好執行表達式並獲取實際數據時,這是您枚舉的時候,所以最后的 ToList() 將 go 並運行查詢,以您期望的格式返回數據,List,而不是 IQueryable

當然,此時有一個明確的消息是,您的 API 端點不應該返回 IQueryable,因為它還不是數據。

因此,您的代碼將變為:

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);           
    }

一個很好的閱讀在這里: https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/action-results

如果我是你,我會從簡單的開始,獲取演示 controller,確保你可以點擊默認操作,然后用你自己的修改它,注意路線,確保它們正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM