c# - linq搜索多列

标签 c# linq linq-to-sql

我想使用 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/

相关文章:

c# - 使用 Moq 在模拟对象中定义方法实现

c# - LINQ Lambda,使用列表分组

c# - 使用 TimeZoneInfo 类来说明夏令时的时间变化

c# - foreach 循环内更快的数据检查和更新

vb.net - .NET - 非常奇怪的 NullReferenceException?

c# - 在 Dynamic Linq 中使用 Any() 和 Count()

c# - 没有第三类的 Linq to SQL 多对多关系

c# - 从代码隐藏添加的 JS 代码无法运行

c# - 从表单更新 LINQ 模型的最佳方法

linq-to-sql - 具有多个连接、分组依据和计数的 LINQ to SQL