Skip to content

Instantly share code, notes, and snippets.

@JeremyLikness
Created May 26, 2020 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeremyLikness/7cbf85231b5d75a5c3fa16d09ec1c4a4 to your computer and use it in GitHub Desktop.
Save JeremyLikness/7cbf85231b5d75a5c3fa16d09ec1c4a4 to your computer and use it in GitHub Desktop.
An example of LINQ at work
using System;
using System.Collections.Generic;
using System.Linq;
namespace linqexample
{
public class Thing
{
private static int NextInt = 1;
private int _id;
public int Id
{
get
{
Console.WriteLine("Accessing id.");
return _id;
}
set => _id = Id;
}
public String Stuff { get; set; }
public Thing()
{
Id = NextInt;
NextInt++;
Stuff = Guid.NewGuid().ToString();
}
}
class Program
{
static void Main(string[] args)
{
var things = new List<Thing>();
var count = 100000;
while (count-- > 0)
{
things.Add(new Thing());
}
Console.WriteLine("Let's ask for 99...");
var thing = things.Where(x => MyMethod(x)).First();
Console.WriteLine($"Found it! Stuff={thing.Stuff}");
Console.ReadLine();
}
private static bool MyMethod(Thing t)
{
return t.Id == 99;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment