jueves, 11 de agosto de 2011

Linq & Lamda syntaxis example

var query = from q in model.Marca
select q;
foreach (var q in query)
Console.WriteLine(q.Descripcion);


var query = model.Marca;
foreach (var q in query)
Console.WriteLine(q.Descripcion);


var query = from q in model.Marca
where q.ClaveMarca == "005"
select q;
foreach (var q in query)
Console.WriteLine(q.Descripcion);


var query = model.Marca.Where(q => q.ClaveMarca == "005");
foreach (var q in query)
Console.WriteLine(q.Descripcion);


var query = from q in model.Marca
where q.ClaveMarca == "005"
select q.Descripcion;
foreach (var q in query)
Console.WriteLine(q);


var query = model.Marca.Where(q => q.ClaveMarca == "005").Select(q => q.Descripcion).Distinct();
foreach (var q in query)
Console.WriteLine(q);


var query = model.Marca.Where(q => q.ClaveMarca == "005").Distinct();
foreach (var q in query)
Console.WriteLine(q.Descripcion);


var query = model.Marca.FirstOrDefault(q => q.ClaveMarca == "005");
Console.WriteLine(query.Descripcion);


var query = model.Marca.Where(q => q.ClaveEntidad == 0).FirstOrDefault();
Console.WriteLine(query.Descripcion);


var query = (from q in model.Marca
where q.ClaveMarca == "005"
select q).FirstOrDefault() ;
Console.WriteLine(query.Descripcion);


var query = from q in model.Marca
where q.ClaveMarca == "005"
select new {
q.ClaveMarca,
q.Descripcion
};
foreach(var q in query)
Console.WriteLine(q);


var query = from q in model.ClaveVehicularSFA
where q.ClaveEntidad == 85
select new
{
cv = q.ClaveVehicular,
m = q.Marca.Descripcion,
l = q.Linea.Descripcion
};
foreach (var q in query)
Console.WriteLine(q);


var query = from q in model.ClaveVehicularSFA
where q.Marca.Descripcion == "NISSAN"
select new
{
q.ClaveVehicular,
q.Marca.Descripcion
};
foreach (var q in query)
Console.WriteLine(q);


var query = model.ClaveVehicularSFA.Where(q => q.Marca.Descripcion == "NISSAN").Select(q => new { q.ClaveVehicular, q.Marca.Descripcion });
foreach (var q in query)
Console.WriteLine("{0} {1}", q.ClaveVehicular, q.Descripcion);


var query = from q in model.ClaveVehicularSFA.Include("Marca").Include("Linea")
where q.ClaveVehicular == "0040702"
select q;
foreach (var q in query)
Console.WriteLine("{0} {1} {2}", q.ClaveVehicular, q.Marca.Descripcion, q.Linea.Descripcion);


var query = model.ClaveVehicularSFA.Include("Marca").Include("Linea").Where(q => q.ClaveVehicular == "0040702");
foreach (var q in query)
Console.WriteLine("{0} {1} {2}", q.ClaveVehicular, q.Marca.Descripcion, q.Linea.Descripcion);


var query = model.ClaveVehicularSFA.Where(q => q.ClaveVehicular == "0040702");
foreach (var q in query)
{
if (!q.MarcaReference.IsLoaded)
q.MarcaReference.Load();
Console.WriteLine("{0} {1}", q.ClaveVehicular, q.Marca.Descripcion);
}


var query = model.ClaveVehicularSFA.Include("Marca").Include("Linea").Include("Version").Where(q => q.ClaveVehicular == "0040702");
foreach (var q in query)
Console.WriteLine("{0} {1} {2} {3}", q.ClaveVehicular, q.Marca.Descripcion, q.Linea.Descripcion, q.Version.Descripcion);


var query = from q in model.ClaveVehicularSFA
where q.ClaveVehicular == "0040702"
select new
{
cv = q.ClaveVehicular,
m = q.Marca.Descripcion,
l = q.Linea.Descripcion,
v = q.Version.Descripcion
};
foreach (var q in query)
Console.WriteLine("{0} {1} {2} {3}", q.cv, q.m, q.l, q.v);


var query = (from q in model.ClaveVehicularSFA
where q.ClaveVehicular == "0040702"
select new
{
cv = q.ClaveVehicular,
m = q.Marca.Descripcion,
l = q.Linea.Descripcion,
v = q.Version.Descripcion
}).FirstOrDefault();
Console.WriteLine("{0} {1} {2} {3}", query.cv, query.m, query.l, query.v);


var query = from q in model.ClaveVehicularSFA
where q.Marca.ClaveMarca == "004"
select new
{
m = q.Marca.Descripcion,
l = q.Linea.Descripcion,
};
foreach(var q in query)
Console.WriteLine("{0} {1}", q.m, q.l);


var query = from c in model.ClaveVehicularSFA
from t in c.TipoFormato
where c.ClaveVehicular == "0040702" && t.NumeroFormato == 9
select c;
foreach (var q in query)
foreach(var t in q)
Console.WriteLine(t.Descripcion);


var query = from c in model.ClaveVehicularSFA
from t in c.TipoFormato
where c.ClaveVehicular == "0040702" && t.NumeroFormato == 9
select c;
if (query.Count() > 0)
Console.WriteLine("estan relacionadas");
else
Console.WriteLine("No estan relacionadas");


//INSERT
var p = new Puerta();
p.ClaveEstadoEntidad = 1;
p.Comentarios = "";
p.CveEntOficinaCambio = 2;
p.CveEntUsuarioCambio = 2;
p.Descripcion = "Descripcion";
p.FechaCambio = DateTime.Now;
p.IPCambio = "127.0.0.1";
model.AddToPuerta(p);
model.SaveChanges();
var query = from q in model.Puerta
select q;
foreach (var q in query)
Console.WriteLine("{0} {1}", q.ClaveEntidad, q.Descripcion);
var query = (from q in model.Puerta
where q.ClaveEntidad == 50
select q).FirstOrDefault();
//Console.WriteLine("{0} {1}", query.ClaveEntidad, query.Descripcion);


//OBJECT 1
var ph = new PuertaHist();
ph.ClaveEstadoEntidad = 1;
ph.Comentarios = "comentarios";
ph.CveEntOficinaCambio = 2;
ph.CveEntUsuarioCambio = 2;
ph.Descripcion = "ph-descripcion";
ph.FechaCambio = DateTime.Now;
ph.IPCambio = "127.0.0.1";
ph.Puerta = query;
model.AddToPuertaHist(ph);
model.SaveChanges();
var query2 = from q2 in model.PuertaHist.Include("Puerta")
select q2;
foreach(var q2 in query2)
Console.WriteLine("{0} {1} {2}", q2.ClaveEntidad, q2.Descripcion, q2.Puerta.ClaveEntidad);

//OBJECT 2
var ph = new PuertaHist();
ph.ClaveEstadoEntidad = 1;
ph.Comentarios = "comentarios";
ph.CveEntOficinaCambio = 2;
ph.CveEntUsuarioCambio = 2;
ph.Descripcion = "ph-descripcion";
ph.FechaCambio = DateTime.Now;
ph.IPCambio = "127.0.0.1";
ph.PuertaReference.EntityKey = new EntityKey(model.DefaultContainerName + ".Puerta", "ClaveEntidad", (byte)49);

model.AddToPuertaHist(ph);
model.SaveChanges();
var query2 = from q2 in model.PuertaHist.Include("Puerta")
select q2;
foreach (var q2 in query2)
Console.WriteLine("{0} {1} {2}", q2.ClaveEntidad, q2.Descripcion, q2.Puerta.ClaveEntidad);


// OBJECT 1 & 2
var p = new Puerta();
p.ClaveEstadoEntidad = 1;
p.Comentarios = "";
p.CveEntOficinaCambio = 2;
p.CveEntUsuarioCambio = 2;
p.Descripcion = "Descripcion";
p.FechaCambio = DateTime.Now;
p.IPCambio = "127.0.0.1";

var ph = new PuertaHist();
ph.ClaveEstadoEntidad = 1;
ph.Comentarios = "comentarios";
ph.CveEntOficinaCambio = 2;
ph.CveEntUsuarioCambio = 2;
ph.Descripcion = "ph-descripcion";
ph.FechaCambio = DateTime.Now;
ph.IPCambio = "127.0.0.1";
ph.Puerta = p;

model.AddToPuertaHist(ph);
model.SaveChanges();
var query = from q in model.PuertaHist.Include("Puerta")
select q;
foreach (var q in query)
Console.WriteLine("{0} {1} {2} {3}", q.ClaveEntidad, q.Descripcion, q.Puerta.ClaveEntidad, q.Puerta.Descripcion);


//OTRA SINTAXIS
var querypersona = from p in personas
where p.Edad>5
orderby p.Nombre, p.Apellido1, p.Apellido2
select new
{
p.Nombre,
p.Apellido1,
p.Apellido2,
p.Edad,
NombreCompleto =
String.Format("{0} {1} {2}",
p.Nombre,
p.Apellido1,
p.Apellido2)
};


//UPDATE
var query = (from q in model.Puerta
where q.ClaveEntidad == 51
select q).FirstOrDefault();
query.Descripcion = "Nueva descrip.";
model.SaveChanges();
var query2 = (from q2 in model.Puerta
where q2.ClaveEntidad == 51
select q2).FirstOrDefault();
Console.WriteLine(query2.Descripcion);

More examples: 101 Linq Examples