2016-10-19 25 views
0

我有一個搜索條件,以基於兩種信息來查找有關車輛的信息。第一個是容器名稱和自定義聲明一個搜索按鈕和兩個文本框如果語句邏輯

我有一個帶有文本搜索和兩個文本框的按鈕,第一個文本是容器名稱,第二個是自定義聲明。

我使用EF加入三個表與包含這樣的:

var query = (from con in db.Containers 
      join v in db.Vehicles on con.cont_vehicleid equals v.vehl_VehicleID 
      join cust in db.Custom_Captions on v.vehl_state equals cust.Capt_Code 
      where cust.Capt_Family == "vehl_state" && v.vehl_Deleted == null && con.cont_Deleted == null && 
      v.vehl_ClearanceCompany == p.pusr_CompanyId && con.cont_Name.Contains(txtContNo.Text) 
      select new 
      { 
       cont_name = con.cont_Name, 
       vehl_Name = v.vehl_Name, 
       VehicleState = v.vehl_state, 
       vehl_drivername = v.vehl_drivername, 
       vehl_entrancedate = v.vehl_entrancedate, 
       vehl_customsdec = v.vehl_customsdec, 
       cont_rampid = v.vehl_rampid 
      }).ToList(); 



var query2 = (from con in db.Containers 
       join v in db.Vehicles on con.cont_vehicleid equals v.vehl_VehicleID 
       join cust in db.Custom_Captions on v.vehl_state equals cust.Capt_Code 
       where cust.Capt_Family == "vehl_state" && v.vehl_Deleted == null && con.cont_Deleted == null && 
       v.vehl_ClearanceCompany == p.pusr_CompanyId && con.cont_customdec.Contains(txtCust.Text) 
       select new 
       { 
        cont_name = con.cont_Name, 
        vehl_Name = v.vehl_Name, 
        VehicleState = v.vehl_state, 
        vehl_drivername = v.vehl_drivername, 
        vehl_entrancedate = v.vehl_entrancedate, 
        vehl_customsdec = v.vehl_customsdec, 
        cont_rampid = v.vehl_rampid 
       }).ToList(); 

與第一查詢包含容器名稱條件(txtContNo.Text)

與第二QUERY2包含條件表單容器名稱(txtCust.Text)

這是我用來處理執行哪個查詢的if語句:

if (txtContNo.Text != null) 
{ 
    rptVehl.DataSource = query; 
    rptVehl.DataBind(); 
} 
if (txtCust.Text != null) 
{ 
    rptVehl.DataSource = query2; 
    rptVehl.DataBind(); 
} 

rptVehl是中繼器工具

我每次編譯代碼中沒有錯誤,但是當我在(txtContNo.Text)輸入容器名稱沒有出現數據,也當我使用的跟蹤點的代碼首先如果屬於(txtContNo.Text)的語句沒有命中。

當我輸入自定義聲明它工作並給我關於我搜索的數據?

請注意:如果我省略第一個if語句,第二個工作正常 並給我所需的輸出。

我認爲if邏輯有問題。有人可以弄清楚嗎?

+0

您可以查詢一般爲兩個條件。如果用戶在兩個文本框中輸入值,那麼你的if邏輯將不起作用。 –

+0

@SanthoshNayak我該怎麼做,請解釋一下? –

+0

放入斷點並檢查txtContNo中是否有值。當你搜索文本 –

回答

1

沒試過,你可以嘗試這樣的事:

var query = (from con in db.Containers 
      join v in db.Vehicles on con.cont_vehicleid equals v.vehl_VehicleID 
      join cust in db.Custom_Captions on v.vehl_state equals cust.Capt_Code 
      where cust.Capt_Family == "vehl_state" && v.vehl_Deleted == null && con.cont_Deleted == null && 
      v.vehl_ClearanceCompany == p.pusr_CompanyId &&(con.cont_Name.Contains(txtContNo.Text==null?con.cont_Name:txtContNo.Text)||con.cont_customdec.Contains(txtCust.Text==null?con.cont_customdec:txtCust.Text)) 
      select new 
      { 
       cont_name = con.cont_Name, 
       vehl_Name = v.vehl_Name, 
       VehicleState = v.vehl_state, 
       vehl_drivername = v.vehl_drivername, 
       vehl_entrancedate = v.vehl_entrancedate, 
       vehl_customsdec = v.vehl_customsdec, 
       cont_rampid = v.vehl_rampid 
      }).ToList(); 



    rptVehl.DataSource = query; 
    rptVehl.DataBind(); 
+0

非常感謝你,ithworks與txtContNo.Text 罰款,但是當我搜索txtCust.Text它給了我兩個記錄與中繼器中的相同信息? –

+0

修改條件 –

+0

這是什麼意思? –

1

我會做的LINQ的一個微小變化,並使其之一:

var query = (from con in db.Containers 
      join v in db.Vehicles on con.cont_vehicleid equals v.vehl_VehicleID 
      join cust in db.Custom_Captions on v.vehl_state equals cust.Capt_Code 
      where cust.Capt_Family == "vehl_state" && v.vehl_Deleted == null && con.cont_Deleted == null && 
      v.vehl_ClearanceCompany == p.pusr_CompanyId 

      select new 
      { 
       cont_name = con.cont_Name, 
       vehl_Name = v.vehl_Name, 
       VehicleState = v.vehl_state, 
       vehl_drivername = v.vehl_drivername, 
       vehl_entrancedate = v.vehl_entrancedate, 
       vehl_customsdec = v.vehl_customsdec, 
       cont_rampid = v.vehl_rampid 
      }).ToList(); 

if(txtConNo.Text != "") 
{ 
    query = query.Where(con => con.cont_Name.Contains(txtContNo.Text)) 
} 

if(txtCust.Text != "") 
{ 
    query = query.Where(con => con.cont_customdec.Contains(txtCust.Text)) 
} 

最後,綁定到GridView

rptVehl.DataSource = query; 
rptVehl.DataBind(); 
+0

你可以在條件? –

+0

con 'con.cont_Name.Contains(txtContNo.Text)' –

+0

已更新。現在它會起作用。感謝您通知@Santhosh Nayak。 –

0

首先,我想說,感謝您爲每個有助於回答我的問題的人Stion的。

我已經修改了AT-2016和Santhosh納亞克

所以我的作品100%的代碼。

var query = (from con in db.Containers 
        join v in db.Vehicles on con.cont_vehicleid equals v.vehl_VehicleID 
        join cust in db.Custom_Captions on v.vehl_state equals cust.Capt_Code 
        where cust.Capt_Family == "vehl_state" && v.vehl_Deleted == null && con.cont_Deleted == null && 
        v.vehl_ClearanceCompany == p.pusr_CompanyId && con.cont_Name.Contains(txtContNo.Text == null ? con.cont_Name : txtContNo.Text) 
        select new 
        { 
         cont_name = con.cont_Name, 
         vehl_Name = v.vehl_Name, 
         VehicleState = v.vehl_state, 
         vehl_drivername = v.vehl_drivername, 
         vehl_entrancedate = v.vehl_entrancedate, 
         vehl_customsdec = v.vehl_customsdec, 
         cont_rampid = v.vehl_rampid 
        }).ToList(); 


var query2 = (from con in db.Containers 
         join v in db.Vehicles on con.cont_vehicleid equals v.vehl_VehicleID 
         join cust in db.Custom_Captions on v.vehl_state equals cust.Capt_Code 
         where cust.Capt_Family == "vehl_state" && v.vehl_Deleted == null && con.cont_Deleted == null && 
         v.vehl_ClearanceCompany == p.pusr_CompanyId && con.cont_customdec.Contains(txtCust.Text == null ? con.cont_customdec : txtCust.Text) 
         select new 
         { 
          cont_name = con.cont_Name, 
          vehl_Name = v.vehl_Name, 
          VehicleState = v.vehl_state, 
          vehl_drivername = v.vehl_drivername, 
          vehl_entrancedate = v.vehl_entrancedate, 
          vehl_customsdec = v.vehl_customsdec, 
          cont_rampid = v.vehl_rampid 
         }).ToList(); 




    if (txtContNo.Text != "") 
    { 
     rptVehl.DataSource = query; 
     rptVehl.DataBind(); 


    } 


    if (txtCust.Text != "") 
    { 
     rptVehl.DataSource = query2; 
     rptVehl.DataBind(); 


    } 

我不會選擇這個作爲一個公認的答案,非常感謝你 AT-2016和Santhosh納亞克