C#搜索查询逗号分隔的访问数据库字段

时间:2014-02-26 23:40:57

标签: c# linq

我正在使用Visual Studio创建一个带有查询的C#程序,该查询将在逗号分隔的访问数据库字段中搜索值。

例如,一条记录的数据库字段可以是A,B,C或C,B代表另一条记录,A代表另一条记录。如果txtDept = C,查询将在前两个记录中成功,但在第三个记录中不成功。这是我提出的代码,但Visual Studio在txtDept上给出了"cannot implicitly convert type 'string' to 'bool'"的错误。

在将数据库字段与txtDept进行比较之前,有没有办法拆分数据库字段?

有人可以帮我提出有效的查询吗?

var courses = from crs in trainingLogDataSet.Course
    where txtDept in crs.Departments
    orderby crs.Date
    select crs;
foreach (var crs in courses)
{
    do something
}

2 个答案:

答案 0 :(得分:1)

您的错误似乎来自您的where子句。

where txtDept in crs.Departments应为where crs.Departments.Contains(txtDept)

答案 1 :(得分:0)

也许是这样的:

var courses = from crs in trainingLogDataSet.Course
                  where crs.Departments.Contains(txtDept)
                  orderby crs.Date
                  select crs
相关问题