asp.net / C#:在EF中使用.contains进行搜索时的奇怪输出

时间:2016-11-27 15:39:27

标签: c# asp.net entity-framework

我有一个转发器,它有两个搜索框,一个用于容器编号,另一个用于自定义声明和一个按钮,当用户搜索容器编号时,它给出了我预期的输出但是当搜索自定义声明时输出显示重复作为两个记录。

这是搜索按钮的代码:

protected void btnSearch_Click(object sender, EventArgs e)
    {
    PortalUser p = (PortalUser)Session["userName"];



        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 = cust.Capt_AR,
                         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();


}

我认为条件中的问题:

con.cont_Name.Contains(txtContNo.Text == null ? con.cont_Name : txtContNo.Text) || con.cont_customdec.Contains(txtCust.Text == null ? con.cont_customdec : txtCust.Text)

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我应该用这样的大括号包围两个.contain:

 (con.cont_Name.Contains(txtContNo.Text == null ? con.cont_Name : txtContNo.Text) || con.cont_customdec.Contains(txtCust.Text == null ? con.cont_customdec : txtCust.Text))