Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Poor Man's LINQ in Visual Studio 2005

, 28 Oct 2010
A way to use LINQ to Objects in C# 2.0 with .NET Framework 2.0
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
//
// Authors:
//        Alejandro Serrano "Serras" (trupill@yahoo.es)
//

using System;
using System.Collections;
using System.Collections.Generic;

namespace Compatibility.Linq
{
        public class Lookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>
        {
                Dictionary<TKey, IGrouping<TKey, TElement>> groups;
                
                internal Lookup (Dictionary<TKey, List<TElement>> groups)
                {
                        this.groups = new Dictionary<TKey, IGrouping<TKey, TElement>> ();
                        foreach (KeyValuePair<TKey, List<TElement>> group in groups)
                                this.groups.Add (group.Key, new Grouping<TKey, TElement>(group.Key, group.Value));
                }
                
                public int Count {
                        get { return groups.Count; }
                }
                
                public bool Contains (TKey key)
                {
                        return groups.ContainsKey (key);
                }
                
                public IEnumerable<TElement> this [TKey key]
                {
                        get { return groups[key]; }
                }
                
                public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator ()
                {
                        return groups.Values.GetEnumerator ();
                }
                
                IEnumerator IEnumerable.GetEnumerator ()
                {
                        return groups.Values.GetEnumerator ();
                }
        }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Qwertie
Software Developer Trapeze Software, Inc.
Canada Canada
Since I started programming when I was 11, I wrote the SNES emulator "SNEqr", the FastNav mapping component, and LLLPG, among other things. Now I'm old.
 
In my spare time I'm developing a system called Loyc (Language of your choice), which will include an enhanced C# compiler. Many programs have an add-in architecture; why not your programming language? I'm also looking for a life partner. Oh hi future wife! Wazzap.

| Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 29 Oct 2010
Article Copyright 2008 by Qwertie
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid