using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
public static class ConsoleSandbox
{
public class PropA
{
public int a { get; set; }
public int b { get; set; }
}
public class PropX
{
public int a { get; set; }
public int b { get; set; }
}
private static void Main()
{
List<PropA> first = new List<PropA>();
List<PropX> second = new List<PropX>();
int numItems = 5000;
Random generator = new Random(0);
for (int i = 0; i < numItems; i++)
{
first.Add(new PropA() { b = i });
second.Add(new PropX() { b = i });
}
first.Shuffle(generator);
second.Shuffle(generator);
Stopwatch watch = new Stopwatch();
watch.Start();
var query = from firstItem in first
join secondItem in second
on firstItem.b equals secondItem.b
select firstItem;
Console.WriteLine(query.Count());
var firstDuration = watch.Elapsed;
Console.WriteLine(watch.Elapsed);
watch.Reset();
watch.Start();
var query2 = first.Where(a => second.Any(x => x.b == a.b));
Console.WriteLine(query2.Count());
Console.WriteLine(watch.Elapsed);
Console.WriteLine();
Console.WriteLine(firstDuration - watch.Elapsed);
Console.WriteLine();
Console.WriteLine("press any key to exit . . .");
Console.ReadKey();
}
public static void Shuffle<T>(this IList<T> list, Random generator)
{
for (int i = list.Count - 1; i > 0; i--)
{
int next = generator.Next(i + 1);
T temp = list[i];
list[i] = list[next];
list[next] = temp;
}
}
}