Linq distinct record containing keywords

前端 未结 12 2079
醉话见心
醉话见心 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:53

    Fiuu, this was brain-wreck. I splited query in several pieces, but it's executed as a whole at the end (var result). And I returned anonymous class, but intention is clear.

    Here is the solution:

    var keywordIds = from k in keywordQuery
                        where splitKeywords.Contains(k.Name)
                        select k.Id;
    
    var matchingKac = from kac in keywordAdCategories
                where keywordIds.Contains(kac.Keyword_Id)
                select kac;
    
    var addIDs = from kac in matchingKac
                    group kac by kac.Ad_Id into d
                    where d.Count() == splitKeywords.Length
                    select d.Key;
    
    var groupedKac = from kac in keywordAdCategoryQuery
                    where addIDs.Contains(kac.Ad_Id)
                    group kac by new { kac.Category_Id, kac.Ad_Id };
    
    var result = from grp in groupedKac
                    group grp by grp.Key.Category_Id into final
                    join c in categoryQuery on final.Key equals c.Id
                    select new
                    {
                        Id = final.Key,
                        Name = c.Name,
                        SearchCount = final.Count()
                    };
    
    // here goes result.ToList() or similar
    

提交回复
热议问题