Introduction to LINQ
Language Integrated Query (LINQ) is a query
syntax that defines a set of query operators that allow traversal, filter, and
projection operations to be expressed in a declarative way in any .NET-based
programming language. It provides a unified programming model for querying and
updating data from different types of data sources and extends data capabilities
directly into the C# and Visual Basic languages. LINQ simplifies the interaction
between object-oriented programming and relational data by applying the
principles of object-oriented programming to relational data.
With the advent of LINQ, a
ground-breaking, new concept of a query has been introduced as a
first-class language construct in C# and Visual Basic. LINQ simplifies the way
you work with data queries. LINQ offers you a unified, declarative syntax model
to query any data source including an XML document, SQL database, an ADO.NET
Dataset, an in-memory collection, or any other remote or local data source that
chooses to support LINQ Language Integrated Queries are strongly typed and
designer tools can be used to create object-relational mappings. It is easier
now for developers to catch many errors during compile-time; also supports
Intellisense and debugging.
Sample Query
A query is an expression that retrieves data
from a data source. All LINQ query operations consist of three essential
actions: Obtain the data source,Create the query and Execute the
query.
In LINQ, the execution of the query is
isolated from the query itself and hence data cannot be retrieved just by
creating a query;
In the following sample, a query retrieves
even numbers from an array of integers.
// Data source.
int[] numbers = new int[10] { 0, 1, 2, 3,
4, 5, 6 ,8,9,10};
// Query creation.
IEnumerable<int> numQuery
=
from num in numbers
where (num % 2) == 0
select num;
// Query execution.
foreach (int j in numQuery)
{
Console.Write("{0,1} ", j);
}
Output will be:
0,2,4,6,8,10
LinqDataSource Control in
ASP.NET
As many of you familiar with various
DataSource controls in ASP.NET 2.0, the 2008 version of ASP.NET includes a new
DataSource control called LinqDataSource control. The LinqDataSource control
enables us to retrieve, update data using Language Integrated Queries (LINQ)
from an in-memory collection of data or SQL Server database tables. It
automatically generates data commands for select, update, delete and insert
operations and you need not have to create them manually.
This control has two main
properties:
1. ContextTypeName property that represents the name of the type object
that contains data collection
2. TableName property that represents the name of the public field or
property that returns data collection, or table name in case of database
access.
Walkthrough 1: Connecting
in-memory data collection using LinqDataSource control and perform queries to
display dat in a ASP.Net page
Open Visual Studio 2008 and create a New
WebSite Project.
Add a
new class file to the App_Code folder of the project. Define classes that
supplies data to the control and write LINQ Queries to retrieve data from
them.
public class Student
{
public string First { get; set;
}
public string Last { get; set;
}
public int ID { get; set; }
public List<int>
Scores;
}
public class StudentData
{
static List<Student> students = new
List<Student>
{
new Student {First="Svetlana",
Last="Omelchenko", ID=111, Scores= new List<int> {97, 92, 81,
60}},
new Student {First="Claire",
Last="O’Donnell", ID=112, Scores= new List<int> {75, 84, 91,
39}},
new Student {First="Sven",
Last="Mortensen", ID=113, Scores= new List<int> {88, 94, 65,
91}},
new Student {First="Cesar",
Last="Garcia", ID=114, Scores= new List<int> {97, 89, 85,
82}},
new Student {First="Debra",
Last="Garcia", ID=115, Scores= new List<int> {35, 72, 91,
70}},
new Student {First="Fadi",
Last="Fakhouri", ID=116, Scores= new List<int> {99, 86, 90,
94}},
new Student {First="Hanying",
Last="Feng", ID=117, Scores= new List<int> {93, 92, 80,
87}},
new Student {First="Hugo",
Last="Garcia", ID=118, Scores= new List<int> {92, 90, 83,
78}},
new Student {First="Lance",
Last="Tucker", ID=119, Scores= new List<int> {68, 79, 88,
92}},
new Student {First="Terry",
Last="Adams", ID=120, Scores= new List<int> {99, 82, 81,
79}},
new Student {First="Eugene",
Last="Zabokritski", ID=121, Scores= new List<int> {96, 85, 91,
60}},
new Student {First="Michael",
Last="Tucker", ID=122, Scores= new List<int> {94, 92, 91, 91}
}
};
You have
just added a Student Class and an initialized list of students in the class
file. The data source for the queries is a simple list of Student objects. Each
Student record has a first name, last name, and an array of integers that
represents their test scores in the class.
Creating Queries
You can
create number of queries to retrieve data from the datasource and in this
article, I have create few queries for demonstration purposes. Each query is
assigned to a public property or field so that it would be used in the
LinqDataSource Controls in the WebPage.
1. Queries of IEnumerable<StudentAverage>
type
public class StudentAverage
{
public int ID;
public string First;
public string Last;
public double ScoreAverage;
}
public IEnumerable<StudentAverage>
studentAverageMarks = from student in StudentData.students
where
student.Scores[0] > 90
select
new StudentAverage { ID=student.ID, First=student.First, Last=student.Last,
ScoreAverage = student.Scores.Average()};
Declared a new type StudentAverage that
consists of ID,First, Last, ScoreAverage fields. This query returns the list of
students with average marks.
2. Queries of IEnumerable<int> type
public static int studentID;
public IEnumerable<int>
GetStudentTotal = from student in StudentData.students where
student.ID == StudentData.studentID select
student.Scores.Sum();
Declared
another public property that returns a int type of collection that holds the
result from the query. This query retrieves sum total of all marks for a
particular student and it uses a condition in the where clause. User will supply
data to the parameter studentID during runtime.
public IEnumerable<int> GetStudentList =
from student
in
StudentData.students select student.ID;
GetStudentList is another property added to the class that stores the
result of a query. This query returns a collection of Student.ID.
3. Queries of Implicit Type(Anonymos Type)
public IEnumerable GetData
{
get
{
var studentMarks = from student in StudentData.students
where
student.ID > 110 select new { ID = student.ID, First =
student.First, Last = student.Last, ScoreTotal = student.Scores.Sum()
};
foreach (var s in studentMarks)
yield return s.ID + ", " + s.Last +
", " +
s.First + ", " + s.ScoreTotal;
}
}
Now you
have just added a public property that returns a query result as collection of
Student’s marks data. Note that the query involves an anonymous type that
contains fields such as ID, First, Last and ScoreTotal. The query returns
implicit type var and it uses Iterators to return the elements in the
collection.
By this time, you have your data sources and
queries ready for access by the data-bound controls of a web page.
|