Introduction
In this article we will learn LINQ. The article explains what LINQ is, why to use LINQ and how to use LINQ. When there was already ADO.NET technology with .NET then why another data access technology? What are the problems solved using LINQ? These are all the things in this article.
The following are the topics covered:
In this article we will learn LINQ. The article explains what LINQ is, why to use LINQ and how to use LINQ. When there was already ADO.NET technology with .NET then why another data access technology? What are the problems solved using LINQ? These are all the things in this article.
The following are the topics covered:
1.What LINQ is .
2.How to write a LINQ Query
What LINQ is
Language Integrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages, including Java, PHP, JavaScript and ActionScript. Let's try to understand what exactly "Language Integrated" means. LINQ is part of programming language syntax. And query means LINQ is used for querying the data. The data would be any type, relational type data and objects too. So we can say that a LINQ is a Programming language syntax used to query data.
Let's try to understand what ADO.NET is then why LINQ. ADO.NET is an object-oriented library and uses data from a relational perspective. We can bind ADO.NET objects directly into a user interface. But we often need a translation, like ADO.NET data into Business Objects with relationships, rules and semantics that do not translate automatically from a relational store. LINQ does remove these kinds of problems and re-shape our data as an object.
LINQ also defines a set of methods names called standard query operators, or standard sequence operators along with translation rules used by the compiler to translate a fluent style of query expressions using these methods names, lambda expressions and anonymous types.
Language Integrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages, including Java, PHP, JavaScript and ActionScript. Let's try to understand what exactly "Language Integrated" means. LINQ is part of programming language syntax. And query means LINQ is used for querying the data. The data would be any type, relational type data and objects too. So we can say that a LINQ is a Programming language syntax used to query data.
Let's try to understand what ADO.NET is then why LINQ. ADO.NET is an object-oriented library and uses data from a relational perspective. We can bind ADO.NET objects directly into a user interface. But we often need a translation, like ADO.NET data into Business Objects with relationships, rules and semantics that do not translate automatically from a relational store. LINQ does remove these kinds of problems and re-shape our data as an object.
LINQ also defines a set of methods names called standard query operators, or standard sequence operators along with translation rules used by the compiler to translate a fluent style of query expressions using these methods names, lambda expressions and anonymous types.
How to write LINQ Query
Before writing a LINQ query we should understand the basic syntax of LINQ. There are the following three ways to write a LINQ query.
Before writing a LINQ query we should understand the basic syntax of LINQ. There are the following three ways to write a LINQ query.
1.Query Syntax
2.Method Syntax
3.Combination of query syntax and method syntax
Query Syntax
This is the basic and simplest way to write a query using query syntax. In query syntax we must use a query expression. In a query expression the first expression demonstrates how to filter records by applying a whereclause, this will return all the elements determined by the condition passed in the where clause.
Example
This is the basic and simplest way to write a query using query syntax. In query syntax we must use a query expression. In a query expression the first expression demonstrates how to filter records by applying a whereclause, this will return all the elements determined by the condition passed in the where clause.
Example
- List<int> marksOfStudent = new List<int>() { 98,45,32,88,59,76,69,90,89,73,66,12,30,26,55};
- IEnumerable<int> query1 =
- from marks in marksOfStudent
- where marks < 30
- select marks;
- IEnumerable<int> query2 =
- from marks in marksOfStudent
- where marks < 30 || num > 65
- orderby num ascending
- select num;
- string[] StudentName = { "Rajeev", "Ranjan", "Prerana", "Nisa", "Rohit" };
- IEnumerable<IGrouping<char, string>> student =
- from item in StudentName
- group item by item[0];
Method Syntax
When a query is written with a method call and returns a single numeric value, like Max, SUM, Min and many others. These methods must always be called last of any query because they represent only by a single value and cannot serve as the source for an additional query.
Example
Example
- List<int> studentClass1 = new List<int>() { 58, 94, 71, 55, 92, 89, 76, 87, 12, 30 };
- List<int> StudentClass2 = new List<int>() { 25, 54, 87, 67, 90, 66, 77, 44, 92, 23 };
- double average = studentClass1.Average();
- IEnumerable<int> concatenationQuery = studentClass1.Concat(studentClass2);
- var average = numbers1.Average();
- var concatenationQuery = numbers1.Concat(numbers2);
Combination of query syntax and method syntax
Mixed Query and Method Syntax will help to use a single query with a method as well as a simple query.
Example
Example
- int marks = (from num in studentClass1
- where num < 33 || num > 65
- select num).Count();
- IEnumerable<int> marks = from num in studentClass1
- where num < 33 || num > 65
- select num;
- int num2 = numbersQuery.Count();
Summary
In this article we learned the basics of LINQ and the syntax of LINQ. Thanks for reading this articlepleases drop comment to let me about the article
No comments :
Post a Comment