使用数组

时间:2015-07-06 05:05:53

标签: c# linq

我有一个图像列表,我想搜索具有两个规则的多个关键字

例如,如果我搜索跳舞的孩子"我想显示一个包含关键字dance和child

的项目列表

我实现了类似这样的查询:

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();

但是这会返回target_keywords中任何项目的结果,例如如果我寻找跳舞的孩子&#34;上面的代码返回任何关键字舞蹈或儿童的项目列表。但我希望列表只包含舞蹈和儿童关键词

那么如何转换查询以便获取包含BOTH关键字的所有记录?

3 个答案:

答案 0 :(得分:1)

System.Linq.Enumerable :: All就是你想要的。

using System.Linq;
using System.Collections.Generic;

struct ImageItem {
    public string Title { get; set; }
    public string Name { get; set; }
}

bool Contains(string toSearch, string x) {
    return toSearch != null && toSearch.ToLower().Contains(x);
}

IEnumerable<ImageItem> FilterItems(IEnumerable<string> targetKeywords, IEnumerable<ImageItem> items) {
    return items.Where(item => targetKeywords.All(x => Contains(item.Name, x) || Contains(item.Title, x)));

}

答案 1 :(得分:-1)

试试这个: -

您必须在语法中用全部替换任何关键字 还有两个字段中所有关键字的排名条件

target_keywords.Any(替换为target_keywords.All(

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.All(x => imageItem.Name != null && imageItem.Name.ToLower().Contains(x)) ? 4 :   
                                   target_keywords.All(x => (imageItem.Name != null && imageItem.Name.ToLower().Contains(x)) || imageItem.Title != null && imageItem.Title.ToLower().Contains(x)) ? 3 :                                
                                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();

答案 2 :(得分:-3)

class ImageDemo
    {
        public string Title { get; set; }
        public string Name { get; set; }
    }

    static void TestCode()
    {
        List<string> target_keywords = new List<string>(){"dancing","child"};
        List<ImageDemo> images = new List<ImageDemo>()
        {  
            new ImageDemo{Title = "dancing"}  ,
             new ImageDemo{Name = "child"}  ,
             new ImageDemo{Title = "child", Name="dancing"}  ,
             new ImageDemo{Title = "dancing", Name="child"}  ,
             new ImageDemo{Name="dancing child"} ,
             new ImageDemo{Title="dancing child"} 
        };
        var searchFuncs = target_keywords.Select(x =>
        {
            Func<ImageDemo, bool> func = (img) =>
            {
                return (img.Title ?? string.Empty).Contains(x) || (img.Name ?? string.Empty).Contains(x);
            };
            return func;
        });
        IEnumerable<ImageDemo> result = images;
        foreach (var func in searchFuncs)
        {
            result = result.Where(x => func(x));
        }
        foreach (var img in result)
        {
            Console.WriteLine(string.Format("Title:{0}  Name:{1}", img.Title, img.Name));
        }
    }

这是你现在想要的正确代码吗?