dimanche 5 juillet 2015

Linq query with All rule in array

I have a list of images and i want to search for multiple keywords with a BOTH rule For example if i search for "dancing child" i want to show a list of items with both keywords dancing and child I implemented a similar querry with below code

List<string> target_keywords = //an array contains Keywords to Lookup 
var RuleAny_results = (from imageItem in images                     
                       select new{ imageItem,
                             Rank =target_keywords.Any(x => imageItem.Title != null && imageItem.Title.ToLower().Contains(x)) ? 5 :
                                   target_keywords.Any(x => imageItem.Name != null && imageItem.Name.ToLower().Contains(x)) ? 4 :                                  
                                0
                         }).OrderByDescending(i => i.Rank);
//exclude results with no match (ie rank=0 ) and get a Distinct set  of items
_searchResult = (from item in RuleAny_results
             where item.Rank != 0
             select item.imageItem).Distinct().ToList();

But this will return results with Any of the items in the target_keywords.ie if i search for "dancing child" above code returns list of items with any of the keywords dancing or child . But i want the list with Both dancing and child keywords only

So how can i convert the querry so that it fetch all records that contains BOTH keywords

Aucun commentaire:

Enregistrer un commentaire