1

我有两张桌子

EmpInf
    EmpId,
    EmpName,
    Salary,
    DepartNumber.

Dept
    DeptNo,
    Deptname,

我也有一个listview1dropdownlist1哪个是必然的EmpInf.EmpName

在传递特定查询时

FilterControl.DataClasses1DataContext obj = new DataClasses1DataContext();
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
    var a = from r in obj.EmpInfs join s in obj.Dept1s on r.DeptNumber equals s.DeptNo where r.EmpName == "'" + DropDownList1.SelectedValue + "'" select s;

    ListView1.DataSource = a;
    ListView1.DataBind();   
}

每当我从下拉列表中选择特定名称时,它都会返回No data were returned。我遗漏了哪些特定代码或是否有任何其他错误?

4

2 回答 2

1

放置一个断点SelectedIndexChanged1并查看 的值SelectedValue以确保DropDownList1.SelectedValue具有 Employee 名称。你也可以试试DropDownList1.SelectedText

string selected = DropDownList1.SelectedValue.ToString();

// e = employee | d = department
var query =
            from   e in obj.EmpInfs
            join   d in obj.Dept1s on e.DeptNumber equals d.DeptNo
            where  e.EmpName == "'" + DropDownList1.SelectedValue + "'"
            select d;

更改此行:

where  e.EmpName == "'" + DropDownList1.SelectedValue + "'"

对此:

where  e.EmpName == selected

好的,我在这里的最后一次尝试......在数据绑定之前执行此操作:

ListView1.DataSource = query.ToList();
于 2012-07-03T12:41:02.263 回答
0

这里的问题很可能是您在 Where 子句中的搜索条件上添加了单引号:

var a = from r in obj.EmpInfs 
        join s in obj.Dept1s on r.DeptNumber equals s.DeptNo 
        where r.EmpName == "'" + DropDownList1.SelectedValue + "'" 
        select s; 

假设 DropDownList1.SelectedValue 是“Smith”,那么您生成的 sql 将类似于:

SELECT *
FROM <tables>
WHERE EmpName = ''Smith''

注意双单引号。要仔细检查这一点,请在生成查询后放置一个断点,然后对其调用 .ToString() 以获取等效的 TSQL。要解决此问题,请从 LINQ 查询中删除“'”,因为它将自动为字符串参数添加:

var a = from r in obj.EmpInfs 
        join s in obj.Dept1s on r.DeptNumber equals s.DeptNo 
        where r.EmpName == DropDownList1.SelectedValue 
        select s;
于 2012-07-03T14:53:01.453 回答