//Select Comun:
//Lambda:
var SelectLambda = db.Customers.ToList();
//Expresión:
var SelectLinq = (from item in db.Customers
select item).ToList();
//Where
//Lambda:
var WhereLambda = db.Customers.Where(c => c.Country.Trim() == "Mexico").ToList();
//Expresión:
var WhereLinq = (from item in db.Customers
where item.Country.Trim() == "Mexico"
select item).ToList();;
//Join
//Expresión:
var JoinLinq = (from item in db.Customers
join p1 in db.Orders on item.CustomerID equals p1.CustomerID
where item.Country.Trim() == "Mexico"
select new
{
Id = item.CustomerID,
OrderID = p1.OrderID
}).ToList();
//Left Join
//Expresión:
var LeftJoinLinq = (from item in db.Customers
join p1 in db.Orders on item.CustomerID equals p1.CustomerID into resultado
from rta in resultado
where item.Country.Trim() == "Mexico"
select new
{
Id = item.CustomerID,
OrderID = (rta != null) ? rta.OrderID : 0
}).ToList();
lunes, 22 de octubre de 2012
Unos ejemplos de LINQ
Acá hay unos pocos ejemplos de LINQ con Entity Framework y la clase Northwind
Suscribirse a:
Enviar comentarios (Atom)
Muy bueno!
ResponderEliminarEn la linea
from rta in resultado
le agregue el metodo .DefaultIfEmpty() y funciono perfectamente.