LINQ2SQL查询匹配多个关键字

时间:2013-05-07 22:01:03

标签: c# linq-to-sql windows-phone-8

我的情况是我需要用逗号分解用户的输入字符串并查询本地数据库以查找至少匹配一个子字符串的记录。

到目前为止,我没有成功,以下不起作用并抛出NotSupportedException

DBRef db = new DBRef { ObjectTrackingEnabled = false };
var ex = Query.ToLower( ).Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
var result = (from a in db.Table where ex.Any(word => a.Title.ToLower().Contains(word)) select a).Take(50);

详情有例外:

An exception of type 'System.NotSupportedException' occurred in System.Data.Linq.ni.dll and wasn't handled before a managed/native boundary
System.NotSupportedException: Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator.
   at System.Data.Linq.SqlClient.QueryConverter.CoerceToSequence(SqlNode node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitQuantifier(Expression sequence, LambdaExpression lambda, Boolean isAny)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp)
   at System.Data.Linq.SqlClient.QueryConverter.VisitWhere(Expression sequence, LambdaExpression predicate)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitTake(Expression sequence, Expression count)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
   at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

这可以在Windows Phone上完成,如果是,那么如何?

0 个答案:

没有答案