2014-02-26 24 views
1

我正在使用Visual Studio创建一个C#程序,该查询将在逗号分隔的访问数据库字段中搜索值。用逗号分隔的访问数据库字段的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 
} 

回答

1

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

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

0

也许是这样的:

var courses = from crs in trainingLogDataSet.Course 
        where crs.Departments.Contains(txtDept) 
        orderby crs.Date 
        select crs 
+0

他们是相同的:) – Leron

相关问题