Linq distinct record containing keywords

前端 未结 12 2095
醉话见心
醉话见心 2021-02-19 05:09

I need to return a distinct list of records based on a car keywords search like: \"Alfa 147\"

The problem is that, as I have 3 \"Alfa\" cars, it returns 1 + 3 records (i

12条回答
  •  说谎
    说谎 (楼主)
    2021-02-19 05:56

    Maybe this is close? At least the subqueries open it up a little for you to work with.

    var query =
      from c in categoryQuery
      let keywords =
      (
        from k in keywordQuery where splitKeywords.Contains(k.Name)
        join kac in keywordAdCategoryQuery on k.Id equals kac.Keyword_Id
        where kac.Category_Id == c.Id
        join a in adQuery on kac.Ad_Id equals a.Id
        select k.Id
      ).Distinct()
      where keywords.Any()
      select new CategoryListByKeywordsDetailDto
      {
        Id = c.Id,
        Name = c.Name,
        SearchCount =
        (
          from kac in keywordAdCategoryQuery
          where kac.Category_Id == c.Id
          join kId in keywords on kac.Keyword_Id equals kId
          select kac.Id
        ).Distinct().Count(),
        ListController = c.ListController,
        ListAction = c.ListAction
      };
    

提交回复
热议问题