亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從匹配條件的行集中獲取不同的值

如何從匹配條件的行集中獲取不同的值

C#
料青山看我應如是 2022-06-12 11:15:29
我有一張以下性質的表格。+----+-----------+-----------+------+---------+------+| 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();


查看完整回答
反對 回復 2022-06-12
  • 1 回答
  • 0 關注
  • 126 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號