给定以下Order
课程:
class Order
{
public string Size {get; set;}
public string Gender {get; set;}
public string Colour {get; set;}
public string Type {get; set;}
// List of "searchable" properties
public IEnumerable<string> GetTags()
{
return new []{Size, Gender, Colour, Type};
}
}
一个简单的方法可能如下所示:
var list = new []
{
new Order {Size = "large", Gender = "women", Colour = "red", Type = "tshirt"},
new Order {Size = "large", Gender = "men", Colour = "black", Type = "tshirt"},
new Order {Size = "medium", Gender = "kids", Colour = "black", Type = "tshirt"},
new Order {Size = "medium", Gender = "kids", Colour = "black", Type = "shorts"},
new Order {Size = "medium", Gender = "men", Colour = "black", Type = "tshirt"}
};
var searchString = "kids black medium";
var searchValues = searchString.Split(new []{" "}, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray();
var result = from order in list
let tags = order.GetTags()
where searchValues.All(s => tags.Any(t => t == s))
select order;
result
现在包含
您也可以使用Intersect
or aHashSet
来比较搜索值/标签,但没有更多的需求信息All
/Any
恕我直言,这是一个足够好的解决方案。