Click here to Skip to main content
15,881,139 members
Articles / Programming Languages / C#

Poor Man's LINQ in Visual Studio 2005

Rate me:
Please Sign up or sign in to vote.
4.53/5 (18 votes)
28 Oct 2010MIT3 min read 81K   619   34  
A way to use LINQ to Objects in C# 2.0 with .NET Framework 2.0
using System;
using System.Collections.Generic;
using System.Text;
using Compatibility.Linq;

namespace Demo
{
	class Program
	{
		public static PoorMansLinq<T> Linq<T>(IEnumerable<T> source)
		{
			return new PoorMansLinq<T>(source);
		}
		public static void Main(string[] args)
		{
			string[] words = new string[] { "Pies", "Are", "Good", "In", "Lovely", "Apples" };
			// Pies Are Good
			Console.WriteLine(string.Join(" ", Linq(words).Take(3).ToArray()));
			// Apples Are Good In Lovely Pies
			Console.WriteLine(string.Join(" ", Linq(words).Sorted().ToArray()));

			int[] numbers = new int[] { 4, 95, 309, 357, 233, 2 };
			// 1000
			Console.WriteLine(Enumerable.Sum(numbers));
			// 666
			Console.WriteLine(Enumerable.Sum(Linq(numbers)
				.Where(delegate(int x) { return x > 300; })));
		}
	}
}

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


Written By
Software Developer None
Canada Canada
Since I started programming when I was 11, I wrote the SNES emulator "SNEqr", the FastNav mapping component, the Enhanced C# programming language (in progress), the parser generator LLLPG, and LES, a syntax to help you start building programming languages, DSLs or build systems.

My overall focus is on the Language of your choice (Loyc) initiative, which is about investigating ways to improve interoperability between programming languages and putting more power in the hands of developers. I'm also seeking employment.

Comments and Discussions