3

我正在尝试创建一种搜索方法,我想检查关键字是否包含在给定记录的任意数量的不同列中。

我的 Linq 语句如下:

string[] searchFilter = {"john", "iceberg"};


var q = from qua in qual
                    join del in deliverables on qua.ID equals del.Q_ID
                    where searchFilter.All(s => (qua.Name + " " + qua.Project + " " + qua.Summary + " " + del.Name + " " + del.Summary).ToLower().Contains(s))
                    select qua;

但是,我收到一条错误消息,其中指出:“本地序列不能用于查询运算符的 LINQ to SQL 实现中,但 Contains() 运算符除外。”

4

3 回答 3

1

实际上,您不能使用 All 方法,而只能使用 Contains 方法:

var q = from qua in qual
        join del in deliverables on qua.ID equals del.Q_ID
        where searchFilter.Contains(qua.Name) ||  searchFilter.Contains(qua.Project)...
        select qua;

当然,您可以将您的 linq 查询与表达式动态组合(但它可能需要更多的工作),或者使用动态 linq。

于 2012-07-30T13:44:54.830 回答
1

不要以为你有选择:

但是当您停留在“可查询”的世界中时,这只会生成一个 sql 请求(丑陋,但是...)

var q = from qua in qual
        join del in deliverables on qua.ID equals del.Q_ID
        select new{ qua, del};

foreach (var filter in searchFilter)
     q = q.Where(v => (v.qua.Name + " " + v.qua.Project + " " + v.qua.Summary + " " + v.del.Name + " " + v.del.Summary).ToLower().Contains(filter));

var result = q.Select(p => p.qua);
于 2012-07-30T13:59:54.807 回答
0

无论出于何种原因, contains 方法在第二次通过循环时都没有使用过滤结果。最终奏效的方法是:

var SearchColumn = from qua in Qual
               join del in deliverables on qua.Q_ID equals del.Q_ID into left_j
                            from del in left_j.DefaultIfEmpty()
                            select new { qua.Name + " " + qua.Project + " " + qua.Summary + " " + del.Name + " " + del.Summary).ToLower() };

foreach (var f in searchFilter)
{
   var likestr = string.Format("%{0}%", f);
   SearchColumn = SearchColumn.Where(x => SqlMethods.Like(x.Search_Col, likestr));
}
于 2012-08-01T18:28:13.363 回答