我有以下代码:排除从Linq查询排除一切结果
public IList<Tweet> Match(IEnumerable<Tweet> tweetStream, IList<string> match, IList<string> exclude)
{
var tweets = from f in tweetStream
from m in match
where f.Text.ToLowerInvariant().Contains(m)
select f;
var final = from f in tweets
from e in exclude
where !f.Text.ToLowerInvariant().Contains(e.ToLowerInvariant())
select f;
return final.Distinct().ToList<Tweet>();
}
我已经建立还没有包括final
结果集,现在已经幸福地匹配我的测试已添加排除,如果IList<string>exclude
为空,则删除所有项目。
所以这个测试通过,因为它应该:
[TestMethod]
public void Should_exclude_items_from_exclude_list()
{
IEnumerable<Tweet> twitterStream = new List<Tweet>
{
new Tweet("I have a Mazda car"),
new Tweet("I have a ford"),
new Tweet("Mazda Rules"),
new Tweet("My Ford car is great"),
new Tweet("My renault is brill"),
new Tweet("Mazda cars are great")
};
IList<string> matches = new List<string>{"mazda","car"};
IList<string> exclude = new List<string>{"ford"};
Matcher target = new Matcher();
IList<Tweet> actual = target.Match(twitterStream, matches, exclude);
Assert.AreEqual(3, actual.Count);
}
但现在测试失败:
[TestMethod]
public void Should_match_items_either_mazda_or_car_but_no_duplicates()
{
IEnumerable<Tweet> twitterStream = new List<Tweet>
{
new Tweet("I have a Mazda car"),
new Tweet("I have a ford"),
new Tweet("Mazda Rules"),
new Tweet("My Ford car is great"),
new Tweet("My renault is brill"),
new Tweet("Mazda cars are great")
};
IList<string> matches = new List<string>{"mazda","car"};
IList<string> exclude = new List<string>();
Matcher target = new Matcher();
IList<Tweet> actual = target.Match(twitterStream, matches, exclude);
Assert.AreEqual(4, actual.Count);
}
我知道我失去了一些东西很简单,但在代码盯着一个小时后,它不会来到我身边。
@JonSkeet,其实我打你的答案,世界即将结束。 :D – msarchet 2011-02-23 22:25:02
@msarchet:不公平,我的妻子需要帮忙评价一件衣服:)(而且我更喜欢我的答案,因为我现在指定的原因。) – 2011-02-23 22:26:46
@JonSkeet,是的,它是我的干净版 – msarchet 2011-02-23 22:27:23