2011-05-10 124 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(); 

我有這個linq查詢。實際上問題是條件where子句r可選的意思是這些是搜索條件。它們可能包含值或不包含。所以我必須進行檢查,如果(ddlProtocol.selectedIndex!= 0)然後spl.PROTOCOL_ID == Convert.ToInt32(ddlProtocol.SelectedValue)條件工作,否則不工作。如何做到這一點linq中where子句的問題

回答

0

使用的編程語言來構建查詢。

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

感謝上面的解決方案。我想再次感謝 – Pankaj 2011-05-11 04:31:30

0

做轉換到一個變量查詢之前:

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

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

其實我的問題不是這個。查詢工作正常,如果我把這些條件,如if(ddlProtocol.selectedIndex!= 0 && ddlProtocol.SelectedIndex!= 0)。我的問題是,如果我在搜索過程中沒有從下拉列表中選擇任何內容,那麼上面的查詢在沒有應用這個if(ddlProtocol.selectedIndex!= 0 && ddlProtocol.SelectedIndex!= 0)的情況下工作。 – Pankaj 2011-05-10 11:36:51

+0

你可以在where子句中做到這一點。 L2S將評估可以在本地進行評估的內容,並僅根據需要由db評估的內容生成SQL查詢。例如。 _where(protocolID == 0 || spl.ProtocolId == protocolID)&&(someOtherParam == 0 || spl.SomeOtherColumn == someOtherParam)... etc ... _ – KristoferA 2011-05-12 05:59:09