如何从匹配条件的行集中获取不同的值

我有一张以下性质的表格。


+----+-----------+-----------+------+---------+------+

| 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"

]

但我收到以下错误。

http://img3.mukewang.com/62a55a66000103d911770162.jpg我做错了什么?

料青山看我应如是
浏览 109回答 1
1回答

慕姐8265434

您收到错误的原因是您将Where条件应用于仅包含 的投影序列ProjectId。你应该使用Where之前Select。要获取不同的值,请使用以下Enumerable.Distinct方法:return&nbsp;await&nbsp;_context.CustomerSatisfactionResults &nbsp;&nbsp;&nbsp;.Where(x&nbsp;=>&nbsp;x.AccountId&nbsp;==&nbsp;accid&nbsp;&&&nbsp;x.Year&nbsp;==&nbsp;year&nbsp;&&&nbsp;x.Quarter&nbsp;==&nbsp;qurter) &nbsp;&nbsp;&nbsp;.Select(x&nbsp;=>&nbsp;x.ProjectId) &nbsp;&nbsp;&nbsp;.Distinct() &nbsp;&nbsp;&nbsp;.ToListAsync();
打开App,查看更多内容
随时随地看视频慕课网APP