Why we go For LINQ in .net ? CHAPTER 1

ORM—Object-Relational Mapping

LINQ to SQL is considered to be one of Microsoft's new ORM products. So before we start explaining LINQ to SQL, let us first understand what ORM is.
ORM stands for Object-Relational Mapping. Sometimes it is called O/RM, or O/R mapping. It is a programming technique that contains a set of classes that map relational database entities to objects in a specific programming language.

LINQ to SQL fully supports transactions, views, Stored Procedures, and user-defined functions

Linq -Integrate queries to language -

USES :
1.
LINQ PAGING TECHNIQUES

2.LINQ Set Operations :



1.LINQ PAGING TECHNIQUES
   1.   Binding Gridview or data controls very easy to use efficient way using Linq operators 'Take ' and Skip operators

var query = from e in db.Entities where etc etc etc;
var pagedQuery =
   
from e in query.Skip(pageNumber).Take(pageSize)

   
select new
   
{
       
Count = query.Count(),
       
Entity = e
   
};


2.LINQ Set Operations :

Intersect() – gathering the common elements

The Intersect() method gets the common elements from two different enumerable sequences, just like the set logic's intersection operation dictates. Intersections are very useful in determining where two sets overlap (that is, what elements two sets have in common).
: // lets say we have a list of healthy stuff to consume
   2: var healthyStuff = new List<string> { "fruits", "vegetables", "proteins", "simple carbs", "fiber" };

   3:  

   4: // and we have a list of what i consume

   5: var myStuff = new List<string> { "soda", "chips", "proteins", "fat", "sugar" };

   1: var results = myStuff.Intersect(healthyStuff);

   2:  

   3: foreach (var item in results)

   4: {

   5:     Console.WriteLine(item);

   6: }

Union() – combining the unique elements

The Union() method combines the unique elements from two different enumerable sequences, just like the set union operation dictates. Unions are very useful for combining two sets without duplicates. Thus if in our example we wanted to get a list of all the healthy foods and all foods I eat, we could union the two sets.

  1: var results = myStuff.Union(healthyStuff);
   2:  

   3: // this will output soda, chips, proteins, fat, sugar,

   4: // fruits, vegetables, simple carbs, and fiber 

   5: foreach (var item in results)

   6: {

   7:     Console.WriteLine(item);

   8: }

Except() – subtracting the common elements

The Except() method performs a set difference between two sets. That is, A – B yields the items in A minus any items in B that happen to be in A. Any items that were unique to B alone are ignored. Thus, if we wanted to get a list of the food I eat that is NOT healthy food, I could do the set difference between what I eat and the healthy things to eat.

1: // this is a list of the things I eat that are not healthy
   2: // soda, chips, fat, sugar

   3: var results = myStuff.Except(healthyStuff);

   4:  

   5: // this is a list of healthy things that I do not eat

   6: // fruits, vegetables, simple carbs, fiber

   7: results = healthyStuff.Except(myStuff);

Comments