Post on 02-Apr-2015
Julián Enrique Verdezoto Celi
Conceptos Generales
LINQ en objetos DEMO
LINQ y XML DEMO
LINQ y SQL DEMO
LINQ (Language Integrated Query)
API de C# 3.0 focalizada en el acceso a datos.
Múltiples origenes de datos.
Extensibilidad SQL, XML y Objetos.
Facilita relación datos objetos
Integración nativa con los lenguajes que facilitan la productividad y la detección de errores (Validación Sintáctica).
Tipado anónimo para proyecciones en todo tipo de colecciones.
Fuertemente Tipado.
Operadores de consulta pueden usarse en cualquier colección.
Select, from, where, group by, join, etc.
Sentencias para ordenamiento de datos.
OPERADORES DE CONSULTA
var numbers = new int[] {1,2,5,8,12,13,14,15,17,21,23};
var results = from c in numbers where (c % 2) == 0 select c;
SENTENCIAS PARA ORDENAMIENTO
static IEnumerable<Estudiante> CrearEstudiantes(){ return new List<Estudiante> { new Estudiante { Nombre = "Juan", Carrera = "Derecho" }, new Estudiante { Nombre = "Ana", Carrera = "Sistemas" }, new Estudiante { Nombre = "Jenny", Carrera = "Turismo"} };}
var results = from c in CrearEstudiantes() where c.Carrera == "Sistemas“ orderby c.Nombre ascending
select c;
Posee una API para XML sencilla y rápida.
Facilita el trabajo con documentos XML, tanto de entrada como de salida.
XML consultas
var doc = XDocument.Load(Server.MapPath("Customers.xml"));
var results = from c in doc.Descendants("customer") where c.Element("city").Value == “London" select c;
XML creación de estructuras
XElement transformedResults = new XElement("Clientes", from cust in results select new XElement("Contacto", new XAttribute("ID", cus.Element("id").Value), new XAttribute("Nombre", cus.Element("name").Value), new XAttribute("Ciudad", cus.Element("city").Value), new XAttribute("Pai", cus.Element("country").Value)));
Mapea una estructura SQL a una clase .NetTraduce una sentencia LINQ a SQL.Soporta operaciones de tipo insert, update y delete.DataContext, BD fuertemente tipadas.Soporte para jerarquías y relaciones.Chequeos en tiempo de compilación.
AplicattionAplicattion
DlinqDlinq(ADO.NET)(ADO.NET)
SQLServerSQLServer
from c in from c in db.Customersdb.Customerswhere c.City = where c.City = “London”“London”select select new new {c.Name, {c.Name, c.Phone }c.Phone }
select Name, Phoneselect Name, Phonefrom customersfrom customerswhere city = ‘London’where city = ‘London’
LINQ Query
ObjectsSubmitChanges()
SQL Query Rows SQL or Stores Procedures
Services:Services:• Change trackingChange tracking• Concurrency control Concurrency control • Object identityObject identity
DLINQ Designer
Relaciones entre entidades
private System.Nullable<long> _VEH_KILOMETRAJE;private string _VEH_COMENTARIO;
private EntitySet<VHC_PARTE> _VHC_PARTEs;private EntityRef<VHC_MODELO> _VHC_MODELO;private EntityRef<VHC_PROPIETARIO> _VHC_PROPIETARIO;
Consulta
grvVehiculos.DataSource = from veh in Vehiculos.VHC_VEHICULO join pro in dsPropietarios.VHC_PROPIETARIO on veh.PRP_ID equals pro.PRP_ID join mod in dsMarcas.VHC_MODELO on veh.MOD_ID equals mod.MOD_ID select new { VEH_ID = veh.VEH_ID, MAR_NOMBRE = mod.VHC_MARCARow.Mar_Nombre, MOD_NOMBRE = mod.MOD_NOMBRE, PRP_NOMBRES_APELLIDOS = pro.PRP_NOMBRES_APELLIDOS, VEH_ANIO_FABRICACION = veh.VEH_ANIO_FABRICACION, VEH_COLOR = veh.VEH_COLOR, VEH_CIUDAD = veh.VEH_CIUDAD, VEH_PRECIO = veh.VEH_PRECIO, VEH_KILOMETRAJE = veh.VEH_KILOMETRAJE, EXTRAS = veh.GetVHC_PARTERows().OrderBy(p => p.VHP_PARTE), NUMEXTRAS = veh.GetVHC_PARTERows().Count()};
Inserción
VehiculosDataContext objVeh_DB = new VehiculosDataContext();
VHC_VEHICULO insVehiculo = new VHC_VEHICULO(); insVehiculo.MOD_ID = v.MOD_ID; insVehiculo.PRP_ID = (int)v.PRP_ID; insVehiculo.VEH_COLOR = v.VEH_COLOR;
insVehiculo.VEH_CIUDAD = v.VEH_CIUDAD;insVehiculo.VEH_PRECIO = v.VEH_PRECIO;insVehiculo.VEH_KILOMETRAJE =
(long)v.VEH_KILOMETRAJE;insVehiculo.VEH_COMENTARIO = v.VEH_COMENTARIO;
objVeh_DB.VHC_VEHICULOs.Add(insVehiculo);
Actualización
VehiculosDataContext objVeh_DB = new VehiculosDataContext();
var uptVehiculo = objVeh_DB.VHC_VEHICULOs.Single(p => p.VEH_ID == v.VEH_ID); uptVehiculo.MOD_ID = v.MOD_ID; uptVehiculo.PRP_ID = (int)v.PRP_ID; uptVehiculo.VEH_ANIO_FABRICACION = v.VEH_ANIO_FABRICACION; uptVehiculo.VEH_COLOR = v.VEH_COLOR; uptVehiculo.VEH_CIUDAD = v.VEH_CIUDAD; uptVehiculo.VEH_PRECIO = v.VEH_PRECIO; uptVehiculo.VEH_KILOMETRAJE = (long)v.VEH_KILOMETRAJE; uptVehiculo.VEH_COMENTARIO = v.VEH_COMENTARIO;
Eliminación
VehiculosDataContext objVeh_DB = new VehiculosDataContext();
objVeh_DB.VHC_VEHICULOs.Remove(objVeh_DB.VHC_VEHICULOs.Single( p => p.VEH_ID == v.VEH_ID));
Soporta las funciones y Stored Procedures.
Reutilización de SPs
objVeh_DB.InsertarMarcas(strNombre, strPais);
objVeh_DB.ActualizarMarcas(intMarId, strNombre, strPais);
http://weblogs.asp.net/scottgu/ msdn2.microsoft.com/netframework/aa904594.aspxwww.microsoft.com/spanish/msdn/articulos/archivo/041206/voices/LINQ_Project.mspxblogs.microsoft.co.il/blogs/bursteg/archive/tags/VS2008/default.aspx
© 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after
the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.