C# tutorial - linq and data structure

Using linq with data structure

Linq that stands for Language-Integrated Query is a query integrated in C#, VB.NET, and other .NET programming languages. Linq provides simple and unified mechanism to access different types of data sources such as data structure, xml, and database of Microsoft SQL Server. The query syntax of the Linq is shown below:

var results=from item in datasource

[where... ] |

[orderby...] |


select itemToAddToResults;

In this tutorial, you learn to use Linq with a data structure. Now you start with a simple problem. In this problem, you are required to create an array of student objects. This array will be used as a data source in Linq query. Each student has id, name, sex, and score. By using Linq, you are asked to do the following tasks:

1. Sorting the student objects by id in ascending order,

2. Selecting students who have the maximum scores, and

3. Counting the number of female and male students in the data source.

First, you need to create a Console Project in Visual Studio. Then you will create a class called Student. This class has four private member variables: id, name, sex, and score. You also need to define four property members to access the variables. The class has only one constructor to initialize each student object.

class Student
 private int id;
 private string name;
 private string sex;
 private double score;
 public Student(int id,string name,string sex,double score)
   this.id = id;
   this.name = name;
   this.sex = sex;
   this.score = score;
public int ID
  get { return id; }
public string Name
   get { return name; }
public string Sex
   get { return sex; }
public double Score
   get { return score; }

In the Main method, you can create an array of student objects as shown below:

Student st = new Student(1,"ratha","male",10);
Student st1 = new Student(5, "bopha", "female", 5);
Student st2 = new Student(3, "dara", "male", 9);
Student st3 = new Student(4, "kia", "male", 7);
Student st4 = new Student(6, "pic", "male", 10);
Student st5 = new Student(2, "vasna", "female", 4);
Student[] studatasource = new Student[] { st, st1, st2, st3, st4, st5 };

The studatasource array will be used as a data source in the Linq query.

1. Sorting the student objects by id in ascending order

You will write a Linq query with orderby statement to retrieve the student objects from the data source in order.

//query to select all student objects in ascending order

var results = from item in studatasource
orderby item.ID ascending
select item;

If you want to sort the student objects in descending order, you need to change from ascending to descending.

//execute the querys

foreach (var student in results)
  Console.WriteLine("ID={0}\t Name={1} \t Sex={2}\t Score={3}",student.ID,student.Name,student.Sex,student.Score);

2. Selecting students who have the maximum scores

To select students who have the maximum scores, first you need create a new datasource of scores from the original datasource. Then write another query to select the students that have the maximum scores. You will the Max method to get the maximum value in the scores data source.

//query to select all scores

var scores= from item in studatasource
select item.Score;

//query to select student who has the maximum score

var studentGetMaxScores = from item in studatasource
where item.Score==scores.Max()
select item;

//execute query
foreach (var student in studentGetMaxScores)
  Console.WriteLine("ID={0}\t Name={1} \t Sex={2}\t Score={3}", student.ID, student.Name, student.Sex, student.Score);

3. Counting the number of female and male students.

To count the number of female and male students, you need to group the student objects by sex. You wil use the group...by...into statement to place the objects in groups. The Count method can be used to count the number of items in a group.

var groupsBySex= from item in studatasource
orderby item.Sex
group item by item.Sex into groupsbysex
select groupsbysex;

foreach (var g in vgroupsBySex)
  Console.WriteLine("{0}:{1}", g.Key,g.Count());


linq to data structure


Kisan comment


Here you can read great beginner C# LINQ tutorials.

<a href="http://studyoverflow.com/linq/">C# LINQ tutorials</a>


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.