Ver Fonte

2009-07-19 Gonzalo Paniagua Javier <[email protected]>

	* Lookup.cs: when there are no matching elements, return an empty
	enumerable. Fixes bug #523386.


svn path=/trunk/mcs/; revision=138190
Gonzalo Paniagua Javier há 16 anos atrás
pai
commit
f8ca363886

+ 5 - 0
mcs/class/System.Core/System.Linq/ChangeLog

@@ -1,3 +1,8 @@
+2009-07-19 Gonzalo Paniagua Javier <[email protected]>
+
+	* Lookup.cs: when there are no matching elements, return an empty
+	enumerable. Fixes bug #523386.
+
 2009-05-18  Jb Evain  <[email protected]>
 
 	* Enumerable.cs (Max, Min): fix generic versions.

+ 5 - 1
mcs/class/System.Core/System.Linq/Lookup.cs

@@ -44,7 +44,11 @@ namespace System.Linq {
 		}
 
 		public IEnumerable<TElement> this [TKey key] {
-			get { return groups [key]; }
+			get {
+				if (groups.ContainsKey (key))
+					return groups [key];
+				return new TElement [0];
+			}
 		}
 
 		internal Lookup (Dictionary<TKey, List<TElement>> lookup)

+ 14 - 0
mcs/class/System.Core/Test/System.Linq/LookupTest.cs

@@ -68,5 +68,19 @@ namespace MonoTests.System.Linq {
 			Assert.AreEqual (0x00ff00, lookup ["GrEeN"].First ());
 			Assert.AreEqual (0x0000ff, lookup ["Blue"].First ());
 		}
+
+		[Test]
+		public void EmptyResult ()
+		{
+			var lookup = GetColors ().ToLookup (
+				c => c.Name,
+				c => c.Value,
+				StringComparer.OrdinalIgnoreCase);
+
+			var l = lookup ["notexist"];
+			Assert.IsNotNull (l);
+			int [] values = (int []) l;
+			Assert.AreEqual (values.Length, 0);
+		}
 	}
 }