Intro to LINQ

I hadn’t used LINQ much, and I wanted to learn a bit more about how it worked, because it seems very popular, and I didn’t want to be left out in the cold. Plus, the “=>” expression used by LINQ made me seem really smart… to myself.

First, a function to build and return some data:

private List BuildLocations() 
{ 
List Locs = new List() { }; 
Locs.Add(new Location("Pritzker","Chicago","IL",1,1)); 
Locs.Add(new Location("Capitol", "Lansing", "MI", 2, 1)); 
Locs.Add(new Location("Amical", "Traverse City", "MI", 3, 2)); 
Locs.Add(new Location("Blackstone Hotel", "Chicago", "IL", 4, 1)); 
Locs.Add(new Location("Wayne Count Courthouse", "Detroit", "MI", 5, 3));
Locs.Add(new Location("Penobscot", "Detroit", "MI", 6, 3)); 
Locs.Add(new Location("Sears Tower", "Chicago", "IL", 7, 1)); 
return Locs; 
}

The first function I tackle was FindAll, which returns all matching elements. There are two ways to work it. First is to define a seperate function and specifying the criteria in the function, and passing this function into the FindAll method:

private static bool FindLocation(Location Loc) 
{ 
if (Loc.City == "Chicago") 
return true; 
return false; 
}

I prefer the second method, defining the criteria in-line using the Where clause, as it is easier to dynamically change the criteria I’m searching for:

protected void btnFindAll_Click(object sender, EventArgs e) 
{ 
List Locs = BuildLocations(); Response.Write("
Using a defined method"); 
List Results = Locs.FindAll(FindLocation); 
foreach (Location Temp in Results) 
     Response.Write("Location = " + Temp.Name + " in " + Temp.City); 
Response.Write("Using an in-line method"); 
Results = Locs.FindAll(p => p.City == "Lansing"); 
foreach (Location Temp in Results) 
Response.Write("Location = " + Temp.Name + " in " + Temp.City); 
}

Next is FindLast, which returns the last element that matches the criteria:

protected void btnFindLast_Click(object sender, EventArgs e) 
{ 
List Locs = BuildLocations(); 
Location Results = Locs.FindLast(p => p.UserId == 1); 
Response.Write("
Location = " + Results.Name + " in " + Results.City); 
}

The last function I tangle with is Exists. I work through two samples, mainly to satisfy my curiosity. The first verifies that matching data exists, and the second verifies that data does not exist:

protected void Button1_Click(object sender, EventArgs e) 
{
List Locs = BuildLocations(); 
bool Results = Locs.Exists(p => p.State == "MI"); 
Response.Write("Do any records with a State of 'MI' exist? " + Results.ToString()); 
Results = Locs.Exists(p => p.State == "AL"); 
Response.Write("Do any records with a State of 'AL' exist? " + Results.ToString()); 
}

Two links to help out with your LINQ-learning:
http://msdn.microsoft.com/en-us/library/bfed8bca.aspx
http://msdn.microsoft.com/en-us/library/5kthb929.aspx

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: