2017-08-26 46 views
0

我試圖從表中選擇並返回某些列,查詢將忽略該選擇並返回除相關表中相關字段以外的所有列。 如何僅返回選定的列?LINQ選擇語法將返回所有列而不是選定的列

這是我API Controller

[Produces("application/json")] 
    [Route("api/Common")] 
public class CommonController : Controller 
{ 
    private readonly ArtCoreDbContext _context; 

    public CommonController(ArtCoreDbContext context) 
    { 
     _context = context; 
    } 

    [HttpGet] 
    public IEnumerable<IdState> GetState() 
    { 
     return _context.IdState; 
    } 

    [HttpGet("{id}")] 
    public async Task<IActionResult> GetState([FromRoute] int id) 
    { 
     var L2EQuery = await (from i in _context.IdState 
             select new { Id = i.StaeId, i.text }).ToListAsync(); 

     return Ok(L2EQuery); 
    } 
} 

更新

這是模型,

public class IdState 
     { 
      [DatabaseGenerated(DatabaseGeneratedOption.Identity), Key] 
      public int StaeId { set; get; } 
      public int CouyId { set; get; } 
      [Display(Name = "State"), Required] 
      public string text { set; get; } 
      public ICollection<PatReg> State { get; set; } 
      public ICollection<IdCity> TblIdCity { get; set; } 

     } 

和輸出,

[{ 
    "staeId": 1, 
    "couyId": 2, 
    "text": "California", 
    "state": null, 
    "tblIdCity": null 
}, { 
    "staeId": 2, 
    "couyId": 2, 
    "text": "WA", 
    "state": null, 
    "tblIdCity": null 
}, { 
    "staeId": 3, 
    "couyId": 1, 
    "text": "Ajloun", 
    "state": null, 
    "tblIdCity": null 
}, { 
    "staeId": 4, 
    "couyId": 1, 
    "text": "Amman", 
    "state": null, 
    "tblIdCity": null 
}] 

ajax呼叫

使用LINQPad 5我得到的只有選中的列,

LINQPad 5 Output

+0

您如何看到它比ID和文本更多地返回? – Backs

+0

我米用'ajax'並輸出到'console' – JSON

+0

顯示'IdState'類和輸出 – Backs

回答

2

我認爲,你可以調用方法public IEnumerable<IdState> GetState()並返回給你整個表。

也許,你需要調用Task<IActionResult> GetState([FromRoute] int id)

問題是在這裏: url: "/api/Common",你叫錯了方法,添加參數:

url: "/api/Common/123",

或可能:

$(function() { 
     $.ajax({ 
      type: "Get", 
      url: "/api/Common", 
      data: {id: 123}, 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (response) { 
       console.log(response); 
      }, 
      failure: function (response) { 
       alert(response.responseText); 
      }, 
      error: function (response) { 
       alert(response.responseText); 
      } 
     }); 
    }); 
+0

我在問題中添加了路由,不會自動調用路由Task [IActionResult> GetState([FromRoute] int id)' – JSON

+0

使用參數解決它'url:「/ api/Common/123「,' – JSON

+0

如何在這種情況下使參數可選,我試過'公共異步任務 GetState([FromRoute] int?id)'但沒有工作? – JSON

0

可以嗎請試試

public class CommonController : Controller 
    { 

     private readonly ArtCoreDbContext _context; 

     public CommonController(ArtCoreDbContext context) 
     { 
      _context = context; 
     } 

     [HttpGet] 
     public IEnumerable<IdState> GetState() 
     { 

      return _context.IdState; 
     } 
     [HttpGet("{id}")] 
     public async Task<IActionResult> GetState([FromRoute] int id) 
     { 
      var L2EQuery = await (from i in _context.IdState 
            where i.test.equals("abc") 
             select new { Id = i.StaeId, text= i.text }).ToListAsync(); 

      return Ok(L2EQuery); 
     } 
    } 
+0

它看起來不像是一個答案 – Backs

+0

我需要使用'where'來過濾查詢嗎? – JSON

+0

是的,你可以用這個試試 – pankaj

3

您的C#代碼沒有問題,但JavaScript。

您沒有在控制器中指定操作,因此它默認爲無參數HttpGet啓用操作,該操作不會投影任何字段。所以問題在於你的AJAX調用。更正像這樣:

$.ajax({ 
    type: "Get", 
    // Pay attention on the next line ! 
    url: "/api/Common/123", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (response) { 
     console.log(response); 
    }, 
    failure: function (response) { 
     alert(response.responseText); 
    }, 
    error: function (response) { 
     alert(response.responseText); 
    } 
}); 

「123」只是一個例子,但是我所看到的,目前也沒有關係,因爲你不過濾你的LINQ什麼。但是,您必須在URL中指定它,以便可以將其解析爲正確的方法。

+0

Bad call,'GET https:// localhost:44303/api/Common/GetStates/123 404()' – JSON

+0

路由在common上('[Route(「api/Common」 )]')不是'GetStates',不是? – JSON

+1

哎呀,我的壞。我錯誤地認爲控制器上的屬性是'RoutePrefix' ... –

相關問題