2011-05-10 3 views
0
var src = (from s in Db.VCT_SITEs 
         join spl in Db.VCT_SPONSOR_SITE_PRO_LINKINGs on s.SITE_ID equals spl.SITE_ID 
         join l in Db.VCT_SITE_LOCATIONs on spl.LOCATION_ID equals l.LOCATION_ID 
         join spd in Db.VCT_SITE_PROTOCOL_DETAILs on spl.PR_SPONSOR_ID equals spd.PR_SPONSOR_ID 
         join c in Db.VCT_CONTACTs on spd.ADMINISTRATOR_ID equals c.CONTACT_ID 
         where c.FIRST_NAME.StartsWith(txtFirstName.Text.Trim()) && 
         c.LAST_NAME.StartsWith(txtLastName.Text.Trim()) && 
         s.SITE_NAME.Contains(txtSiteName.Text.Trim()) && 
         spl.PROTOCOL_ID==Convert.ToInt32(ddlProtocol.SelectedValue) && 
         l.LOCATION_ID==Convert.ToInt32(ddlLocation.SelectedValue) 
         select new 
            { 
             NAME=c.FIRST_NAME + " " + c.MIDDLE_NAME + " " + c.LAST_NAME, 
             s.SITE_ID, 
             l.LOCATION_NAME, 
             s.PHONE, 
             s.FAX, 
             s.SITE_NAME, 
             s.EMAIL_ID, 
             s.IS_ACTIVE 
          }).AsQueryable(); 

J'ai ce LINQ query.Actually le problème est dans des conditions où la clause optionnelle r signifie ce sont la recherche conditions.they peuvent contenir la valeur ou non. donc je dois faire une vérification comme si (ddlProtocol.selectedIndex! = 0) alors spl.PROTOCOL_ID == Convert.ToInt32 (ddlProtocol.SelectedValue) condition de travail sinon, pas. comment faireproblème dans la clause where dans LINQ

Répondre

0

Utilisez le langage de programmation pour construire la requête.

IQueryable<VCT_Contact> cQuery = Db.VCT_CONTACTs; 
string firstName = txtFirstName.Text.Trim(); 
if (firstName != string.Empty) 
{ 
    cQuery = cQuery.Where(c => c.FIRST_NAME.StartsWith(firstName)); 
} 
string lastName = txtLastName.Text.Trim(); 
if (lastName != string.Empty) 
{ 
    cQuery = cQuery.Where(c => c.LAST_NAME.StartsWith(lastName)); 
} 

IQueryable<VCT_SITE> sQuery = Db.VCT_SITEs; 
string siteName = txtSiteName.Text.Trim(); 
if (siteName != string.Empty) 
{ 
    sQuery = sQuery.Where(s => s.SITE_NAME.Contains(siteName)); 
} 
IQueryable<VCT_SPONSOR_SITE_PRO_LINKING> splQuery = Db.VCT_SPONSOR_SITE_PRO_LINKINGs; 
int protocol = Convert.ToInt32(ddlProtocol.SelectedValue); 
if (protocol != 0) 
{ 
    splQuery = splQuery.Where(spl => spl.PROTOCOL_ID == protocol); 
} 
IQueryable<VCT_SITE_LOCATION> lQuery = Db.VCT_SITE_LOCATIONs; 
int location = Convert.ToInt32(ddlLocation.SelectedValue); 
if (location != 0) 
{ 
    lQuery = lQuery.Where(l => l.LOCATION_ID == location); 
} 

var src = (
    from s in sQuery 
    join spl in splQuery on s.SITE_ID equals spl.SITE_ID 
    join l in lQuery on spl.LOCATION_ID equals l.LOCATION_ID 
    join spd in Db.VCT_SITE_PROTOCOL_DETAILs on spl.PR_SPONSOR_ID equals spd.PR_SPONSOR_ID 
    join c in cQuery on spd.ADMINISTRATOR_ID equals c.CONTACT_ID 
    select new { 
    NAME=c.FIRST_NAME + " " + c.MIDDLE_NAME + " " + c.LAST_NAME, 
    s.SITE_ID, 
    l.LOCATION_NAME, 
    s.PHONE, 
    s.FAX, 
    s.SITE_NAME, 
    s.EMAIL_ID, 
    s.IS_ACTIVE 
    }).AsQueryable(); 
+0

merci beaucoup ci-dessus est le sol. que je veux encore merci beaucoup – Pankaj

0

Est-ce que la conversion en une variable avant la requête:

int protocolId = int.Parse(ddlProtocol.SelectedValue); 

var src = from ... 
where spl.PROTOCOL_ID==protocolID 
    ... 
select ...; 
+0

En fait mon problème n'est pas ceci. La requête fonctionne correctement si je mets ces conditions comme si (ddlProtocol.selectedIndex! = 0 && ddlProtocol.SelectedIndex! = 0). Mon problème est que si je n'ai rien sélectionné dans la liste déroulante pendant la recherche alors la requête ci-dessus fonctionne sans appliquer ceci si (ddlProtocol.selectedIndex! = 0 && ddlProtocol.SelectedIndex! = 0) si la condition. – Pankaj

+0

Vous pouvez le faire dans la clause where. L2S évaluera ce qui peut être évalué localement et générera une requête SQL basée uniquement sur ce qui doit être évalué par la base de données. Par exemple. _where (protocolID == 0 || spl.ProtocolId == protocolID) && (someOtherParam == 0 || spl.SomeOtherColumn == someOtherParam) ... etc ... – KristoferA