我有一張以下性質的表格。+----+-----------+-----------+------+---------+------+| Id | AccountId | ProjectId | Year | Quarter | Data |+----+-----------+-----------+------+---------+------+| 39 | 163 | 60 | 2019 | 2 | 0 || 40 | 163 | 60 | 2019 | 2 | 8 || 41 | 163 | 61 | 2019 | 2 | 1 || 42 | 163 | 61 | 2019 | 2 | 2 |+----+-----------+-----------+------+---------+------+我想ProjectIds使用 Entity Framework 與 Json 不同,到目前為止我的代碼看起來像這樣。 // GET: api/Insight/163/2019/2 [HttpGet("{accid}/{year}/{qurter}")] public async Task<IActionResult> GetSurveys([FromRoute] long accid, [FromRoute] long year, [FromRoute] long qurter) { //This code gives me the error. return await _context.CustomerSatisfactionResults.Select(x=>x.ProjectId) .Where(x => x.AccountId == accid && x.Year == year && x.Quarter == qurter).ToListAsync(); }當我用參數點擊這個端點時,/163/2019/2我想要一個 Json 響應,[ "60", "61"]但我收到以下錯誤。我做錯了什么?
1 回答

慕姐8265434
TA貢獻1813條經驗 獲得超2個贊
您收到錯誤的原因是您將Where
條件應用于僅包含 的投影序列ProjectId
。你應該使用Where
之前Select
。
要獲取不同的值,請使用以下Enumerable.Distinct
方法:
return await _context.CustomerSatisfactionResults .Where(x => x.AccountId == accid && x.Year == year && x.Quarter == qurter) .Select(x => x.ProjectId) .Distinct() .ToListAsync();
- 1 回答
- 0 關注
- 126 瀏覽
添加回答
舉報
0/150
提交
取消