Linq to sql linq中where子句的问题

Linq to sql linq中where子句的问题,linq-to-sql,Linq To Sql,我有这个linq查询。实际上问题是where子句r optional中的条件意味着这些是搜索条件。它们可能包含值,也可能不包含值。所以我必须检查一下,比如ifddlProtocol.selectedIndex=0则spl.PROTOCOL_ID==Convert.ToInt32ddlProtocol.SelectedValue条件有效,否则无效。如何执行此操作在查询之前将其转换为变量: var src = (from s in Db.VCT_SITEs

我有这个linq查询。实际上问题是where子句r optional中的条件意味着这些是搜索条件。它们可能包含值,也可能不包含值。所以我必须检查一下,比如ifddlProtocol.selectedIndex=0则spl.PROTOCOL_ID==Convert.ToInt32ddlProtocol.SelectedValue条件有效,否则无效。如何执行此操作

在查询之前将其转换为变量:

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();

使用编程语言构造查询

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

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

其实我的问题不是这个。如果我像ifddlProtocol.selectedIndex这样设置这些条件,查询工作正常=0&&ddlProtocol.SelectedIndex=0我的问题是,如果我在搜索过程中没有从下拉列表中选择任何内容,那么上面的查询将在不应用此ifddlProtocol.selectedIndex的情况下工作=0&&ddlProtocol.SelectedIndex=0 if条件。您可以在where子句中执行此操作。L2S将评估可在本地评估的内容,并仅基于数据库需要评估的内容生成SQL查询。例如,其中protocolID==0 | | spl.protocolID==protocolID&&someOtherParam==0 | | spl.SomeOtherColumn==someOtherParam…等等。。。
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();