Using a LINQ query in LINQPad

Exclusive offer: get 50% off this eBook here
Building Interactive Queries with LINQPad

Building Interactive Queries with LINQPad — Save 50%

Learn how to query databases interactively using LINQ and LINQPad with this book and ebook

$23.99    $12.00
by Sébastien Finot | September 2013 | .NET Enterprise Articles Microsoft

In this article by Sébastien Finot, the author of the book "Building Interactive Queries with LINQPad", a brief introduction to LINQ and how it can be used has been discussed. LINQPad is a free IDE (Integrated Development Environment) that allows you to create programs and query databases. Language Integrated Query (LINQ) consists of a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic.

To introduce you to LINQ, we will first look into a non-LINQ example so that we can have a point of comparison.

 

(For more resources related to this topic, see here.)

The standard version

We are going to implement a simple scenario: given a deck of 52 cards, we want to pick a random number of cards, and then take out all of the hearts. From this stack of hearts, we will discard the first two and take the next five cards (if possible), and order them by their face value for display.

You can try it in a C# program query in LINQPad:

public static Random random = new Random();
void Main()
{
var deck = CreateDeck();
var randomCount = random.Next(52);
var hearts = new Card[randomCount];
var j = 0;
// take all hearts out
for(var i=0;i<randomCount;i++)
{
if(deck[i].Suit == "Hearts")
{
hearts[j++] = deck[i];
}
}
// resize the array to avoid null references
Array.Resize(ref hearts, j);

// check that we have at least 2 cards. If not, stop
if(hearts.Length <= 2)
return;
var count = 0;
// check how many cards we can take
count = hearts.Length - 2;
// the most we need to take is 5
if(count > 5) { count = 5; }
// take the cards

var finalDeck = new Card[count];
Array.Copy(hearts, 2, finalDeck, 0, count);
// now order the cards
Array.Sort(finalDeck, new CardComparer());
// Display the result
finalDeck.Dump();
}
public class Card
{
public string Suit { get; set; }
public int Value { get; set; }
}
// Create the cards' deck
public Card[] CreateDeck()
{
var suits = new [] { "Spades", "Clubs",
"Hearts", "Diamonds" };
var deck = new Card[52];
for(var i = 0; i < 52; i++)
{
deck[i] = new Card { Suit = suits[i / 13],
FaceValue = i-(13*(i/13))+1 };
}
// randomly shuffle the deck
for (var i = deck.Length - 1; i > 0; i--)
{
var j = random.Next(i + 1);
var tmp = deck[j];
deck[j] = deck[i];
deck[i] = tmp;
}
return deck;
}
// CardComparer compare 2 cards against their face value
public class CardComparer : Comparer<Card>
{
public override int Compare(Card x, Card y)
{
return x.FaceValue.CompareTo(y.FaceValue);
}
}

Even if we didn't consider the CreateDeck() method, we had to do quite a few operations to produce the expected result (your values might be different as we are using random cards). The output is as follows:

Depending on the data, LINQPad will add contextual information. For example, in this sample it will add the bottom row with the sum of all the values (here, only FaceValue).

Also, if you click on the horizontal graph button, you will get a visual representation of your data, as shown in the following screenshot:

This information is not always relevant but it can help you explore your data.

Summary

In this article we saw how LINQ queries can be used in LINQPad. The powerful query capabilitiesof LINQ has been utilized to the maximum in LINQPad.

Resources for Article:


Further resources on this subject:


Building Interactive Queries with LINQPad Learn how to query databases interactively using LINQ and LINQPad with this book and ebook
Published: July 2013
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

About the Author :


Sébastien Finot

Fascinated by computer science on the whole (but with a specific dedication to application development and games), Sébastien Finot has been working for more than 15 years for small and big worldwide companies at various technical positions, ranging from developer to technical project manager or, for the last five years, as the CTO of communication agencies. He is also pursuing an entrepreneurial path with his own company (in France), where he can work on fun and innovative projects. Sébastien has been using LINQ for five years now, and has been a LINQPad user for almost as long.

Sébastien can be reached using any of the means listed on this website: http://about.me/sebastienf.

Books From Packt


WCF 4.5 Multi-Layer Services Development with Entity Framework
WCF 4.5 Multi-Layer Services Development with Entity Framework

WCF Multi-tier Services Development with LINQ
WCF Multi-tier Services Development with LINQ

WCF 4.0 Multi-tier Services Development with LINQ to Entities
WCF 4.0 Multi-tier Services Development with LINQ to Entities

LINQ Quickly
LINQ Quickly

Mastering SQL Queries for SAP Business One
Mastering SQL Queries for SAP Business One

OData Programming Cookbook for .NET Developers
OData Programming Cookbook for .NET Developers

Entity Framework Tutorial
Entity Framework Tutorial

.NET 4.5 Parallel Extensions Cookbook
.NET 4.5 Parallel Extensions Cookbook


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
F
c
j
R
1
p
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software