代码之家  ›  专栏  ›  技术社区  ›  Nishan

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

  •  -2
  • Nishan  · 技术社区  · 7 年前

    我有一张符合自然规律的桌子。

    +----+-----------+-----------+------+---------+------+
    | 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 由于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 Respone AS,

    [
      "60", "61"
    ]
    

    但我得到以下错误。 enter image description here 我做错了什么?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Kristoffer Jälén    7 年前

    出现错误的原因是应用 Where 仅包括 ProjectId . 你应该使用 在哪里? 之前 Select .

    要获取不同的值,请使用 Enumerable.Distinct 方法:

    return await _context.CustomerSatisfactionResults
       .Where(x => x.AccountId == accid && x.Year == year && x.Quarter == qurter)
       .Select(x => x.ProjectId)
       .Distinct()
       .ToListAsync();
    
    推荐文章