我想使用 ling-to-sql 在多个列中搜索一个字符串,我想知道如何编写 where
子句。这就是我所拥有的:我正在传递要搜索的 ID 列表以及搜索词:
public List<long> Seach(string TheSearchTerm, List<long> TheIDs)
{
using (SomeDataContext TheDC = new SomeDataContext())
{
var TheOutput = (from t in TheDC.SomeTable
where TheIDs.Contains(t.ID) &&
where "TheSearchTerm is in one of the columns"
select t.ID).ToList();
}
}
如何编写第二个 where
子句来搜索所有列?我想为每一列写一个 where 子句,但我想知道是否有更好的方法。
谢谢。
最佳答案
var TheOutput = (from t in TheDC.SomeTable
where TheIDs.Contains(t.ID) && (
t.column1.Contains(TheSearchTerm) ||
t.column2.Contains(TheSearchTerm) ||
t.column3.Contains(TheSearchTerm) )
select t.ID).ToList();
}
您应该只有一个 where 子句,并使用 ||
组合所有列的检查。
关于c# - linq搜索多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18292618/