* Some experimenting with linq perforamnce inspired by http://tiredblogger.wordpress.com/2008/07/25/iqueryable-methods-on-activereports-controlcollections/ .
* Author: Scott Willeke, http://blog.scott.willeke.com, scott at willeke dot com
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqWithCollections
{
class Program
{
List<Label> _controlsSimpleList;
IQueryable<Label> _controlsQueryable;
List<Indicator> _indicators;
private bool IsSpanish;
const int TestIterationCount = 10;
const int indicatorCount = 400;
static void Main()
{
Program p = new Program();
p.CreateSampleData();
TimeTest(p.NoLinqTest);
TimeTest(p.WithLinqExtensionMethods);
TimeTest(p.WithLinqQueryable);
Console.ReadLine();
}
private void WithLinqQueryable()
{
var foundCount = 0;
foreach (var indicator in _indicators)
{
var indicatorHeader = string.Format("i{0}", indicator.Id);
var indicatorControl = _controlsQueryable.SingleOrDefault(x => x.Name == indicatorHeader);
if (indicatorControl != null)
{
foundCount++;
indicatorControl.Text = IsSpanish
? indicator.SpanishText
: indicator.EnglishText;
}
}
if (foundCount != indicatorCount/2)
throw new InvalidOperationException("invalid foundCount");
}
private void WithLinqExtensionMethods()
{
var foundCount = 0;
foreach (var indicator in _indicators)
{
var indicatorHeader = string.Format("i{0}", indicator.Id);
var indicatorControl = _controlsSimpleList.SingleOrDefault(x => x.Name == indicatorHeader);
if (indicatorControl != null)
{
foundCount++;
indicatorControl.Text = IsSpanish
? indicator.SpanishText
: indicator.EnglishText;
}
}
if (foundCount != indicatorCount / 2)
throw new InvalidOperationException("invalid foundCount");
}
private void NoLinqTest()
{
var controlLookup = new Dictionary<string, Label>();