我有一个搜索条件,可根据两种信息查找有关车辆的信息。第一个是容器名称和自定义声明
我有一个带文本搜索的按钮和两个文本框,第一个是容器名称文本,第二个是自定义声明。
我使用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)中输入容器名称时,没有数据出现,当我使用tracePoint时,第一个if语句中的代码属于(txtContNo.Text)并没有&# 39; t hit。
当我输入自定义声明时,它有效并向我提供有关我搜索内容的数据吗?
注意:如果省略第一个if语句,第二个工作正常 并给了我想要的输出。
我认为if逻辑存在问题。有人可以搞清楚吗?
答案 0 :(得分: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();
答案 1 :(得分: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();
答案 2 :(得分:0)
首先,我要感谢所有有助于回答我问题的人。
我修改了AT-2016和Santhosh Nayak的代码
所以它对我有100%的作用。
{{1}}
我不会选择这个作为接受的答案,非常感谢你 AT-2016和Santhosh Nayak