一个搜索按钮和两个文本框用于语句逻辑



我有一个搜索条件来查找基于两类信息的车辆信息。首先是容器名称和自定义声明

我有一个按钮与文本搜索和两个文本框,第一个文本的容器名称和第二个自定义声明。

我使用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)

条件的查询

包含容器名称(txtCust.Text)的第二个查询2

,这是我用来处理哪个查询要执行的if语句:

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

rptVehl是中继器工具

每次我编译代码没有错误,但当我在(txtContNo.Text)中输入容器名称时,没有数据出现,也当我使用tracePoint时,第一个if语句中的代码属于(txtContNo.Text)不命中。

当我输入自定义声明时,它工作并给我关于我搜索的数据?

注意:如果我省略了第一个if语句,第二个也可以正常工作并给出我想要的输出。

我认为if逻辑有问题。有人能弄明白吗?

未尝试,您可以尝试这样做:

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

我会在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();

首先我想感谢每一位帮助我回答问题的人。

我已经修改了AT-2016和Santhosh Nayak的代码

所以它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 Nayak

最新更新