Buenas,
Aquí el primero de unos post rápidos de LINQ TO SQL, para aquellos que estamos aprendiendo
. Comenzamos con un SELEC
1.- Select * from —-> xD
T-SQL
SELECT * FROM dbo.EntradasBlog
LINQ TO SQL
VB.NET
Dim [select] = From e In sel.EntradasBlogs Select e
C#
var selec = from e in sel.EntradasBlogs
select e;
2. Select —> en un solo campo
T-SQL
SELECT Titulo FROM dbo.EntradasBlog
LINQ TO SQL
VB.NET
Dim [select] = From e In sel.EntradasBlogs Select e.Titulo
C#
var selec = from e in sel.EntradasBlogs select e.Titulo;
3.- Select —> obtenido el primer registro
T-SQL
SELECT TOP 1 Titulo FROM dbo.EntradasBlog
LINQ TO SQL
VB.NET
Dim [select] = (From e In sel.EntradasBlogs Select e.Titulo).First
C#
var selec = (from e in sel.EntradasBlogs
select e.Titulo).First();
4. Select —> un registro en especifico
T-SQL
SELECT Titulo FROM dbo.EntradasBlog
WHERE id = 1
LINQ TO SQL
VB.NET
Dim [select] = sel.EntradasBlogs.SingleOrDefault _
(Function(c) c.Id = "0000000001")
donde ser es:
Dim sel As New DemosDataContext
ó
Dim [select] = (From e In sel.EntradasBlogs _
Where e.Id = "0000000001" _
Select e.Titulo).Single
C#
var selec = sel.EntradasBlogs.SingleOrDefault(c
=> c.Id == "0000000001");
ó
var selec = (from e in sel.EntradasBlogs
where e.Id == "0000000001"
select e).Single();
5. Select —> un numero especifico de registros
T-SQL
SELECT TOP 3 * FROM dbo.EntradasBlog
LINQ TO SQL
VB.NET
Dim [select] = (From e In sel.EntradasBlogs).Take(3)
C#
var selec = (from e in sel.EntradasBlogs
select e).Take(3);
6. Select—> con subquerys
T-SQL
SELECT Titulo FROM dbo.EntradasBlog
WHERE id = (SELECT id from dbo.Entradas
WHERE (id = '0000000001'))
LINQ TO SQL
VB.NET
Dim [select] = _
(From e In sel.EntradasBlogs _
Where (From en In sel.Entradas _
Where (en.id = "0000000001") _
Select en.id).Contains(e.Id) _
Select e.Titulo)
C#
var selec = from e in sel.EntradasBlogs
where (
from en in sel.Entradas
where en.id == "0000000001"
select en.id
).Contains(e.Id)
select e.Titulo;
Bien parece que estos son algunas maneras de realizar Select desde LINQ2SQL, ya en otros post’s iremos hablando sobre otras sentencias.
Comentarios recientes