From f8ca3638867ae338510fe06db82f64f4c85e1854 Mon Sep 17 00:00:00 2001 From: Gonzalo Paniagua Javier Date: Mon, 20 Jul 2009 02:45:47 +0000 Subject: [PATCH] 2009-07-19 Gonzalo Paniagua Javier * Lookup.cs: when there are no matching elements, return an empty enumerable. Fixes bug #523386. svn path=/trunk/mcs/; revision=138190 --- mcs/class/System.Core/System.Linq/ChangeLog | 5 +++++ mcs/class/System.Core/System.Linq/Lookup.cs | 6 +++++- mcs/class/System.Core/Test/System.Linq/LookupTest.cs | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/mcs/class/System.Core/System.Linq/ChangeLog b/mcs/class/System.Core/System.Linq/ChangeLog index 734a0d07a31..8234a3e1da2 100644 --- a/mcs/class/System.Core/System.Linq/ChangeLog +++ b/mcs/class/System.Core/System.Linq/ChangeLog @@ -1,3 +1,8 @@ +2009-07-19 Gonzalo Paniagua Javier + + * Lookup.cs: when there are no matching elements, return an empty + enumerable. Fixes bug #523386. + 2009-05-18 Jb Evain * Enumerable.cs (Max, Min): fix generic versions. diff --git a/mcs/class/System.Core/System.Linq/Lookup.cs b/mcs/class/System.Core/System.Linq/Lookup.cs index 0398ad6a53a..19affc8433d 100644 --- a/mcs/class/System.Core/System.Linq/Lookup.cs +++ b/mcs/class/System.Core/System.Linq/Lookup.cs @@ -44,7 +44,11 @@ namespace System.Linq { } public IEnumerable this [TKey key] { - get { return groups [key]; } + get { + if (groups.ContainsKey (key)) + return groups [key]; + return new TElement [0]; + } } internal Lookup (Dictionary> lookup) diff --git a/mcs/class/System.Core/Test/System.Linq/LookupTest.cs b/mcs/class/System.Core/Test/System.Linq/LookupTest.cs index 85007758d5f..6f790a5b3c7 100644 --- a/mcs/class/System.Core/Test/System.Linq/LookupTest.cs +++ b/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); + } } } -- 2.11.4.GIT