- Extension methods enable you to extend the methods of an existing type. For example, you could create an extension method to the String class called ToStringArray(), which when called turns a string variable into a string array.
- Lambda expressions enable you to write an expression within a method signature that is evaluated at runtime. Lambda expressions are a shorthand way of writing a delegate.
- LINQ provides classes that contain extension methods commonly referred to as query operators.
- Using extension methods in this way means that any type that inherits IEnumberable can be queried using LINQ.
- Objects that implement IEnumberable are collections of objects or XML documents loaded into memory.
---------------------------------------------------------------------------------------------------------
using System;
using System.Linq;
{
class Program
{
static void Main(String[] args)
{
sting[] names = new string[] { "Simon", "Johe", "Angharad", Tim" };
string[] longnams = GetLongNames(names);
foreach(string name in lognames)
{
Console.WriteLine(name);
}
static string[] GetLongNames(string[] names)
{
var longnams = names.Where(n => n.length > 4);
return longnames.ToArray();
}
}
}
-------------------------------------------------------------------------------------------------------------
Executing this code would return two names: Simon and Angharad. - The Where() method is a query operator defined in Enumerable.
- The lambda expression it executes returns an output collection of string objects longer than four characters. The n variable is of type string because the array names is a string array.
- The var keyword in C# 3.0 is an implicit type. This means that it is strongly typed, taking the return type of the Where() method, which is the output collection of type IEnumerable.
- The query operators under the Queryable class largely mirror those in the Enumerable class, meaning that to developers, the syntax for writing LINQ queries is the same whether you are querying local objects or remote data.
- LINQ to SQL translates LINQ queries into Transact SQL (T- SQL) for SQL Server.
- LINQ to Entities translate a LINQ query into Entity SQL (E- SQL) for the ADO.NET Entity Framework. E- SQL is a special form of SQL used by the ADO.NET Entity Framework to query the conceptual model defined by the ORM(Object Relatational Mapping).
LINQ TO SQL VS. LINQ TO ENTITIES
The key difference between LINQ to SQL and LINQ to Entities is that LINQ to Entities provides a conceptual model for your database, whereas LINQ to SQL works much more closely with the relational data model of your database.
The extra layer of abstraction LINQ to Entities provides from the structure of the database itself enables you to design an object- oriented representation (conceptual model) of your relational database. Queries written in LINQ to Entities are executed against this conceptual model using E- SQL. The entity framework handles mapping between the conceptual model of your entities and the relational model of database using an ORM(Object Relatational Mapping), thus addressing an impedance mismatch that can occur between how you ideally want to model the data in the database and how you want represent an object model in your code.
No comments:
Post a Comment