Saturday, May 24, 2008

LINQ in Visual Studio 2008

LINQ stand for Language-Integrated Query is now available as an integral part of Visual Studio 2008. Visual Studio gives a native syntax to developers in the form LINQ to C# and VB.Net for accessing data from any repository. The repository could be in memory object, database (still MS SQL Server only) and XML files.

what LINQ is doing ?
Basically LINQ address the current database development model in the context of Object Oriented Programming Model. If some one wants to develop database application on .Net platform the very simple approach he uses ADO.Net. ADO.Net is serving as middle ware in application and provides complete object oriented wrapper around the database SQL.

LINQ has a great power of querying on any source of data, data source could be the collections of objects, database or XML files. We can easily retrieve data from any object that implements the IEnumerable interface. Microsoft basically divides LINQ into three areas and that are give below.
o LINQ to Object {Queries performed against the in-memory data}
o LINQ to ADO.Net
o LINQ to SQL (formerly DLinq) {Queries performed against the relation database only Microsoft SQL Server Supported}
o LINQ to DataSet {Supports queries by using ADO.NET data sets and data tables}
o LINQ to Entities {Microsoft ORM solution}
o LINQ to XML (formerly XLinq) { Queries performed against the XML source}

1. Code 1
int[] numbers = new int[] {100,101,102};
var result = from output in numbers where output <>

select output;
foreach(int count in result)
Console.WriteLine(count);
Output:
100
101
102
All type of SQL Operates are available in LINQ to Object. like Sum Operator in the following code.
2. Code 2
int[]numbers = new int[] {2,4,6,2};
int result = nums.Sum();
Console.WriteLine(result);

LINQ to object provided main types of Operator Type that are give below

Operator Types Operator Name
Aggregation • Aggregate • Average • Count • LongCount, • Max, • Min, • Sum

Conversion • Cast, • OfType, • ToArray, • ToDictionary, • ToList,
•ToLookup, ToSequence
Element • DefaultIfEmpty, • ElementAt, • ElementAtOrDefault, • First,
• FirstOrDefault, • Last, • LastOrDefault, • Single, • SingleOrDefault
Equality • EqualAll
Generation • Empty, • Range, • Repeat
Grouping • GroupBy
Joining • GroupJoin, • Join
Ordering • OrderBy, • ThenBy, • OrderByDescending,
• ThenByDescending, • Reverse
Partitioning • Skip, • SkipWhile, • Take, • TakeWhile
Quantifiers • All, • Any, • Contains
Restriction • Where
Selection • Select, • SelectMany
Set • Concat, • Distinct, • Except, • Intersect, • Union

No comments: