Microsoft & .NETVisual C#.NET Tip: Basic Data Manipulation with LINQ

.NET Tip: Basic Data Manipulation with LINQ content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

LINQ provides a flexible means to work with your data that wasn’t available before. You now have access to SQL-like capabilites that you can apply to your own data types or to built-in data types. In this tip, you will learn how to sort your data and how to reshape the data that your LINQ query returns. To begin, set up some data for the examples that follow with a Person class and an array of Person objects:

public class Person
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public int Age { get; set; }
   public string Occupation { get; set; }

Person[] People = new Person[] {
   new Person { FirstName = "Jay", LastName = "Miller",
                Age = 41, Occupation = "Software Engineer" },
   new Person { FirstName = "Bill", LastName = "Gates",
               Age = 52, Occupation = "Billionaire" },
   new Person { FirstName = "George", LastName = "Bush",
                Age = 62, Occupation = "President" }

This code defines a Person class with four properties to hold a first name, last name, age, and occupation. The People variable then is defined as an array of Person objects and initialized with three entries. Start by taking a look at how to sort the data. LINQ provides an orderby clause that you use with the from statement. The first example selects all of the items in People, sorts them by LastName then FirstName, and then outputs them to the Debug window. The code looks like this:

// Sort the list of people by LastName, FirstName
var PeopleByName = from p in People
                   orderby p.LastName ascending,
                   p.FirstName ascending
                   select p;

foreach (var p in PeopleByName)
   Debug.Print(p.LastName + ", " + p.FirstName);

Here is the output from the above example:

Bush, George
Gates, Bill
Miller, Jay

As you would expect, the data was sorted in order by LastName. As you can see, orderby supports sorting on multiple keys and the ability to sort any given key in either ascending or descending order.

The other ability of LINQ I’d like to show you is reshaping the result or your query. In the first example, the select p clause simply returned each data element in its entirety. The select clause also enables you to return a data type that is different from the base data you are querying. In this example, I will return a new data type from the query that includes the Age property from the original data, as well as a Name that is a concatenation of the FirstName and LastName properties.

// Sort the list of people by Age and combine FirstName
// and LastName into a single Name field
var PeopleByAge = from p in People
                  orderby p.Age descending
                  select new { p.Age, Name = p.FirstName + " " +
                               p.LastName };

foreach (var p in PeopleByAge)
    Debug.Print(p.Age + " " + p.Name);

Visual Studio can infer the data type that is returned from the query, so in the foreach loop that outputs each element, IntelliSense is available. This makes it extremely easy to reshape the data depending upon how you need to manipulate it and have full support in the IDE. I also changed the orderby clause to sort the data in descending order by Age so you can see an example of a different sort. The output looks like this:

62 George Bush
52 Bill Gates
41 Jay Miller

There is so much more that LINQ can do for you application. I was a little slow to realize just how big an impact LINQ could have on my applications. I hope that you explore LINQ’s other abilities and find ways to simplify your applications.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories