Profilo di PabloEl blog de Pablo Castill...BlogElenchi Strumenti Guida

Blog


24 aprile

Cómo crear una búsqueda dinámica con linq, la típica con where dinámico

 

¿Cómo se podría hacer con Dlinq el típico formulario de búsqueda por varios campos?

Muy fácil, se puede formar dinámicamente la consulta, al estilo procedimiento almacenado, pero todo desde Visual Studio, depurando y de una forma directa. Creo que realmente esto es un gran progreso respecto a como se realizaba anteriormente, ahí no hay conexiones, entlib.. ni nada por el estilo, se ataca la base de datos de una forma muy directa.

 

 

 

Código del botón buscar de la web (los cb son combos y los tb textbox):

protected void btBuscar_Click(object sender, EventArgs e)
{
var q = from p in db.PROYECTOS
from t in db.TRABAJOS
from pri in db.PRIORIDADES
from est in db.ESTADOS
where p.PROTRABAJO==t.TRAID && p.PROPRIORIDAD==pri.PRIID && p.PROESTADO==est.ESTID
select new{proexpediente=p.PROEXPEDIENTE,protrabajo=t.TRANOMBRE, prodescripcion=p.PRODESCRIPCION,
PRIORIDAD=pri.PRINOMBRE,ESTADO=est.ESTNOMBRE,
CLIID=p.CLIID,PROEXPEDIENTE=p.PROEXPEDIENTE,PRONUMVIVIENDAS=p.PRONUMVIVIENDAS,
LOCID=p.LOCID,CORID=p.CORID,PROFECHAALTA=p.PROFECHAALTA,PROESTADO=p.PROESTADO,PROID=p.PROID};

if (cbClientes.SelectedValue != "0")
{
q = q.Where(h=> h.CLIID == Convert.ToInt32(cbClientes.SelectedValue));
}
if (!string.IsNullOrEmpty(tbExpediente.Text))
{
q = q.Where(h=> h.PROEXPEDIENTE == tbExpediente.Text);
}
if (tbNumViviendas.Text != "0")
{
q = q.Where(h=> h.PRONUMVIVIENDAS == Convert.ToInt32(tbNumViviendas.Text));
}
if (cbLocalidades.SelectedValue != "0")
{
q = q.Where(h=> h.LOCID == Convert.ToInt32(cbLocalidades.SelectedValue));
}
if(cbCoordinador.SelectedValue!="0")
{
q = q.Where(h=> h.CORID == Convert.ToInt32(cbCoordinador.SelectedValue));
}
if(cbAño.SelectedValue!="0")
{
q = q.Where(h=> h.PROFECHAALTA.Year.ToString()== cbAño.SelectedValue);
}
if(!chboxIncluirTerminados.Checked)
{
q = q.Where(h=> h.PROESTADO != 2);
}

GridViewBuscados.DataSource = q;
GridViewBuscados.DataBind();  <-- Aquí ejecuta la consulta en la BBDD
}

 

Todo esto forma un sql muy correcto:

exec sp_executesql N'SELECT [t0].[PRO_EXPEDIENTE] AS [proexpediente], [t1].[TRA_NOMBRE] AS [protrabajo], [t0].[PRO_DESCRIPCION] AS [prodescripcion], [t2].[PRI_NOMBRE] AS [PRIORIDAD], [t3].[EST_NOMBRE] AS [ESTADO], [t0].[CLI_ID] AS [CLIID], [t0].[PRO_NUM_VIVIENDAS] AS [PRONUMVIVIENDAS], [t0].[LOC_ID] AS [LOCID], [t0].[COR_ID] AS [CORID], [t0].[PRO_FECHA_ALTA] AS [PROFECHAALTA], [t0].[PRO_ESTADO] AS [PROESTADO], [t0].[PRO_ID] AS [PROID]
FROM [PROYECTOS] AS [t0], [TRABAJOS] AS [t1], [PRIORIDADES] AS [t2], [ESTADOS] AS [t3]
WHERE ([t0].[PRO_ESTADO] <> @p0) AND ([t0].[PRO_TRABAJO] = [t1].[TRA_ID]) AND ([t0].[PRO_PRIORIDAD] = [t2].[PRI_ID]) AND ([t0].[PRO_ESTADO] = [t3].[EST_ID])',N'@p0 int',@p0=2

 

El linq realmente va a cambiar la forma de hacer los programas....

Commenti (5)

Attendere...
Il commento immesso è troppo lungo. Immetti un commento più breve.
Immissione non effettuata. Riprova.
Impossibile aggiungere il commento al momento. Riprova più tardi.
Per aggiungere un commento è necessaria l'autorizzazione di un genitore. Chiedi autorizzazione
I tuoi genitori hanno disattivato i commenti.
Impossibile eliminare il commento al momento. Riprova più tardi.
Hai raggiunto il numero massimo di commenti pubblicabili giornalmente. Riprova tra 24 ore.
Impossibile lasciare commenti. La funzionalità è stata disattivata perché i sistemi hanno rilevato una possibile attività di spamming dal tuo account. Se ritieni che il tuo account è stato disattivato per errore, contatta il supporto tecnico di Windows Live.
Esegui il seguente controllo di protezione per completare la pubblicazione del commento.
I caratteri digitati nel controllo di protezione devono corrispondere ai caratteri dell'immagine o della riproduzione audio.

Per aggiungere un commento, accedi con il tuo Windows Live ID (se utilizzi Hotmail, Messenger o Xbox LIVE possiedi già un Windows Live ID). Accedi


Non hai ancora un Windows Live ID? Registrati

Senza nomeha scritto:

Hi,Do you need digital signages, advertising displays, digital sign, advertisement displays and advertising players? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.

amberdigital Contact Us

website:www.amberdigital.com.hk
alibaba:amberdigital.en.alibaba.com[djdfhcbgebacfg]

25 Ott.
12 Ott.
Senza nomeha scritto:

Hi,Do you have used lcd screens, lcd monitor used, surplus lcds and scrap LCDs? Please go here:www.sstar-hk.com(Southern Stars).We are constantly buying re-usable LCD panels.The re-usable panels go through strictly designed process of categorizing, checking, testing, repairing and refurbishing before they are re-used to make remanufactured LCD displays and TV sets.Due to our recent breakthrough in testing and repairing technology of LCD, we can improve the value for your LCD panels. website:www.sstar-hk.com[bbjihahfhddbeh]

29 Set.
Senza nomeha scritto:

Hi,Do you need advertising displays, advertising player and LCD displays? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.

amberdigital Contact Us

E-mail:sstar@netvigator.com
website:www.amberdigital.com.hk
alibaba:amberdigital.en.alibaba.com[abihegidbaafhf]

9 Set.
2 Set.

Riferimenti

L'URL di riferimento per questo intervento è:
http://pablocastilla.spaces.live.com/blog/cns!D9A7D79398CDD8FF!767.trak
Blog che fanno riferimento a questo intervento
  • Nessuno