我要实现对某一字段的查询,查询关键字是两个或两个以上的字符串,但查出来的相邻两个关键字的间距必须在20个字符以内,这样可以保持它们关系比较密切。
下面的代码在EF中无法成功,错误是:
| 方法“Boolean IsNearKeys(System.String, System.Collections.Generic.List`1[System.String])”不支持转换为 SQL。 |
| Message |
方法“Boolean IsNearKeys(System.String, System.Collections.Generic.List`1[System.String])”不支持转换为 SQL。 |
| Data |
|
| InnerException |
null |
| TargetSite |
| Visitor.VisitMethodCall (SqlMethodCall mc) |
| Name |
VisitMethodCall |
| DeclaringType |
|
| ReflectedType |
|
| CustomAttributes |
|
| MetadataToken |
100667032 |
| Module |
|
| MethodImplementationFlags |
IL |
| MethodHandle |
| System.RuntimeMethodHandle |
| Value |
|
|
| Attributes |
PrivateScope, Assembly, Virtual, HideBySig, CheckAccessOnOverride |
| CallingConvention |
Standard, HasThis |
| IsGenericMethodDefinition |
False |
| ContainsGenericParameters |
False |
| IsGenericMethod |
False |
| IsSecurityCritical |
False |
| IsSecuritySafeCritical |
False |
| IsSecurityTransparent |
True |
| IsPublic |
False |
| IsPrivate |
False |
| IsFamily |
False |
| IsAssembly |
True |
| IsFamilyAndAssembly |
False |
| IsFamilyOrAssembly |
False |
| IsStatic |
False |
| IsFinal |
False |
| IsVirtual |
True |
| IsHideBySig |
True |
| IsAbstract |
False |
| IsSpecialName |
False |
| IsConstructor |
False |
| MemberType |
Method |
| ReturnType |
|
| ReturnParameter |
| System.Data.Linq.SqlClient.SqlExpression |
| ParameterType |
|
| Name |
null |
| HasDefaultValue |
True |
| DefaultValue |
null |
| RawDefaultValue |
null |
| Position |
-1 |
| Attributes |
None |
| Member |
| Visitor.VisitMethodCall (SqlMethodCall mc) |
|
| IsIn |
False |
| IsOut |
False |
| IsLcid |
False |
| IsRetval |
False |
| IsOptional |
False |
| MetadataToken |
134217728 |
| CustomAttributes |
|
|
| ReturnTypeCustomAttributes |
| System.Data.Linq.SqlClient.SqlExpression |
| ParameterType |
|
| Name |
null |
| HasDefaultValue |
True |
| DefaultValue |
null |
| RawDefaultValue |
null |
| Position |
-1 |
| Attributes |
None |
| Member |
| Visitor.VisitMethodCall (SqlMethodCall mc) |
|
| IsIn |
False |
| IsOut |
False |
| IsLcid |
False |
| IsRetval |
False |
| IsOptional |
False |
| MetadataToken |
134217728 |
| CustomAttributes |
|
|
|
| StackTrace |
在 System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitMethodCall(SqlMethodCall mc) 在 System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) 在 System.Data.Linq.SqlClient.SqlVisitor.VisitExpression(SqlExpression exp) 在 System.Data.Linq.SqlClient.SqlVisitor.VisitSelectCore(SqlSelect select) 在 System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitSelect(SqlSelect select) 在 System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) 在 System.Data.Linq.SqlClient.SqlVisitor.VisitAlias(SqlAlias a) 在 System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) 在 System.Data.Linq.SqlClient.SqlVisitor.VisitSource(SqlSource source) 在 System.Data.Linq.SqlClient.SqlVisitor.VisitSelectCore(SqlSelect select) 在 System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitSelect(SqlSelect select) 在 System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) 在 System.Data.Linq.SqlClient.PostBindDotNetConverter.Convert(SqlNode node, SqlFactory sql, ProviderMode providerMode) 在 System.Data.Linq.SqlClient.SqlProvider.BuildQuery(ResultShape resultShape, Type resultType, SqlNode node, ReadOnlyCollection`1 parentParameters, SqlNodeAnnotations annotations) 在 System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations) 在 System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) 在 System.Data.Linq.DataQuery`1.System.Collections.IEnumerable.GetEnumerator() 在 LINQPad.ObjectGraph.ListNode.<GetItems>d__8.MoveNext() 在 System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) 在 System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 在 LINQPad.ObjectGraph.ListNode..ctor(ObjectNode parent, IEnumerable list, GraphOptions options, String name) 在 LINQPad.ObjectGraph.ObjectNode.CreateInternal(ObjectNode parent, Object item, GraphOptions options) |
| HelpLink |
null |
| Source |
System.Data.Linq |
| HResult |
-2146233067 |
void Main()
{
var keys = new List<string>();
keys.Add("女子");
keys.Add("须发");
var query = from item in CM_BookPageDetails
where IsNearKeys(item.Content,keys)
select item;
query.Take(10).Dump();
}
bool IsNearKeys(string source, List<string> keys)
{
var lastCharIndex = 0;
for(var i=0;i<keys.Count ;i++)
{
var charIndex = source.IndexOf(keys[i]);
if(i>0 && charIndex - lastCharIndex > 20)
return false;
lastCharIndex = charIndex;
}
return true;
}
// Define other methods and classes here