Using LINQ to Objects in C# C# with Example



Using LINQ to Objects in C# C# with Example

A simple SELECT query in Linq 
static void Main(string[] args) 
{ 
string[] cars = { "VW Golf", 
"Opel Astra", 
"Audi A4", 
"Ford Focus", 
"Seat Leon", 
"VW Passat", 
"VW Polo", 
"Mercedes C-Class" }; 
var list = from car in cars 
select car; 
StringBuilder sb = new StringBuilder(); 
foreach (string entry in list) 
{ 
sb.Append(entry + "\n"); 
} 
Console.WriteLine(sb.ToString()); 
Console.ReadLine(); 
} 
In the example above, an array of strings (cars) is used as a collection of objects to be queried using LINQ. In a LINQ 
query, the from clause comes first in order to introduce the data source (cars) and the range variable (car). When 
the query is executed, the range variable will serve as a reference to each successive element in cars. Because the 
compiler can infer the type of car, you do not have to specify it explicitly 
When the above code is compiled and executed, it produces the following result: 
SELECT with a WHERE Clause 
var list = from car in cars 
where car.Contains("VW") 
select car; 
The WHERE clause is used to query the string array (cars) to find and return a subset of array which satisfies the 
 

WHERE clause. 
When the above code is compiled and executed, it produces the following result: 
Generating an Ordered List 
var list = from car in cars 
orderby car ascending 
select car; 
Sometimes it is useful to sort the returned data. The orderby clause will cause the elements to be sorted according 
to the default comparer for the type being sorted. 
When the above code is compiled and executed, it produces the following result: 
Working with a custom type 
In this example, a typed list is created, populated, and then queried 
public class Car 
{ 
public String Name { get; private set; } 
public int UnitsSold { get; private set; } 
public Car(string name, int unitsSold) 
{ 
Name = name; 
UnitsSold = unitsSold; 
} 
} 
class Program 
{ 
static void Main(string[] args) 
{ 
var car1 = new Car("VW Golf", 270952); 
var car2 = new Car("Opel Astra", 56079); 
var car3 = new Car("Audi A4", 52493); 
var car4 = new Car("Ford Focus", 51677); 
var car5 = new Car("Seat Leon", 42125); 
 

var car6 = new Car("VW Passat", 97586); 
var car7 = new Car("VW Polo", 69867); 
var car8 = new Car("Mercedes C-Class", 67549); 
var cars = new List { 
car1, car2, car3, car4, car5, car6, car7, car8 }; 
var list = from car in cars 
select car.Name; 
foreach (var entry in list) 
{ 
Console.WriteLine(entry); 
} 
Console.ReadLine(); 
} 
} 
When the above code is compiled and executed, it produces the following result: 
Until now the examples don't seem amazing as one can just iterate through the array to do basically the same. 
However, with the few examples below you can see how to create more complex queries with LINQ to Objects and 
achieve more with a lot less of code. 
In the example below we can select cars that have been sold over 60000 units and sort them over the number of 
units sold: 
var list = from car in cars 
where car.UnitsSold > 60000 
orderby car.UnitsSold descending 
select car; 
StringBuilder sb = new StringBuilder(); 
foreach (var entry in list) 
{ 
sb.AppendLine($"{entry.Name} - {entry.UnitsSold}"); 
} 
Console.WriteLine(sb.ToString()); 
When the above code is compiled and executed, it produces the following result: 
 

In the example below we can select cars that have sold an odd number of units and order them alphabetically over 
its name: 
var list = from car in cars 
where car.UnitsSold % 2 != 0 
orderby car.Name ascending 
select car; 
When the above code is compiled and executed, it produces the following result: 
 

LINQ is an acronym which stands for Language INtegrated Query. It is a concept which integrates a query language 
by offering a consistent model for working with data across various kinds of data sources and formats; you use the 
same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, 
.NET collections, and any other format for which a LINQ provider is available. 

0 Comment's

Comment Form

Submit Comment