Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 3.50 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Exclude results from Linq query excluding everything when exclude list is empty
  2. public IList<Tweet> Match(IEnumerable<Tweet> tweetStream, IList<string> match, IList<string> exclude)
  3.     {
  4.         var tweets = from f in tweetStream
  5.                      from m in match
  6.                      where f.Text.ToLowerInvariant().Contains(m)
  7.                      select f;
  8.  
  9.         var final = from f in tweets
  10.                     from e in exclude
  11.                     where !f.Text.ToLowerInvariant().Contains(e.ToLowerInvariant())
  12.                     select f;
  13.  
  14.         return final.Distinct().ToList<Tweet>();
  15.     }
  16.        
  17. [TestMethod]
  18.     public void Should_exclude_items_from_exclude_list()
  19.     {
  20.         IEnumerable<Tweet> twitterStream = new List<Tweet>
  21.                                                {
  22.                                                    new Tweet("I have a Mazda car"),
  23.                                                    new Tweet("I have a ford"),
  24.                                                    new Tweet("Mazda Rules"),
  25.                                                    new Tweet("My Ford car is great"),
  26.                                                    new Tweet("My renault is brill"),
  27.                                                    new Tweet("Mazda cars are great")
  28.                                                };
  29.         IList<string> matches = new List<string>{"mazda","car"};
  30.         IList<string> exclude = new List<string>{"ford"};
  31.  
  32.         Matcher target = new Matcher();
  33.         IList<Tweet> actual = target.Match(twitterStream, matches, exclude);
  34.  
  35.         Assert.AreEqual(3, actual.Count);            
  36.     }
  37.        
  38. [TestMethod]
  39.     public void Should_match_items_either_mazda_or_car_but_no_duplicates()
  40.     {
  41.         IEnumerable<Tweet> twitterStream = new List<Tweet>
  42.                                                {
  43.                                                    new Tweet("I have a Mazda car"),
  44.                                                    new Tweet("I have a ford"),
  45.                                                    new Tweet("Mazda Rules"),
  46.                                                    new Tweet("My Ford car is great"),
  47.                                                    new Tweet("My renault is brill"),
  48.                                                    new Tweet("Mazda cars are great")
  49.                                                };
  50.         IList<string> matches = new List<string>{"mazda","car"};
  51.         IList<string> exclude = new List<string>();
  52.  
  53.         Matcher target = new Matcher();
  54.         IList<Tweet> actual = target.Match(twitterStream, matches, exclude);
  55.  
  56.         Assert.AreEqual(4, actual.Count);
  57.     }
  58.        
  59. from e in exclude
  60.        
  61. var final = from f in tweets
  62.             let lower = f.Text.ToLowerInvariant()
  63.             where !exclude.Any(e => lower.Contains(e.ToLowerInvariant())
  64.             select f;
  65.        
  66. var tweets = from f in tweetStream
  67.              let lower = f.Text.ToLowerInvariant()
  68.              where match.Any(m => lower.Contains(m.ToLowerInvariant())
  69.              where !exclude.Any(e => lower.Contains(e.ToLowerInvariant())
  70.              select f;
  71.        
  72. var final = from f in tweets
  73.             from e in exclude
  74.             where !f.Text.ToLowerInvariant().Contains(e.ToLowerInvariant())
  75.             select f;
  76.        
  77. var excludeTheseTweet = from f in tweets
  78.                         from e in exclude
  79.                         where f.Text.ToLowerInvariant().Contains(e.ToLowerInvariant())
  80.                         select f;
  81.  
  82. return tweets.Except(excludeTheseTweets).Distinct().ToList<Tweet>();