[英]Converting LINQ Extension method to work with L2E
I am working on a search page where users are able to use wildcards *
in their search criteria. 我正在搜索页面上,用户可以在搜索条件中使用通配符
*
。 The wildcard can be placed at the beginning, or end of a string. 通配符可以放在字符串的开头或结尾处。 Since there are several fields I need this to be applied to, I figured an extension method would be the best way.
由于我需要将几个字段应用于此,我认为扩展方法是最好的方法。 The code I came up with currently works, but not with
IQueryable
. 我想出的代码目前有效,但不适用于
IQueryable
。
public static class Helper
{
public static IEnumerable<string> MyExtMethod(this IEnumerable<string> items, string searchString)
{
var searchType = -1;
if(searchString.IndexOf("*") == -1) //No wildcard
{
searchType = 0;
}
else if(searchString.IndexOf("*") == 0 && searchString.LastIndexOf("*") == searchString.Length - 1)//start and end
{
searchType = 1;
}
else if(searchString.IndexOf("*") == 0)//ends with
{
searchType = 2;
}
else if(searchString.LastIndexOf("*") == searchString.Length - 1) //starts with
{
searchType = 3;
}
var search = searchString.Replace("*", "");
foreach(var i in items)
{
switch(searchType)
{
case 0: yield return i;
break;
case 1: if(i.Contains(search))
yield return i;
break;
case 2: if(i.EndsWith(search))
yield return i;
break;
case 3: if(i.StartsWith(search))
yield return i;
break;
}
}
}
}
I only use string manipulation and extension methods that are already supported by L2E Contains
, StartsWith
, EndsWith
. 我只使用L2E
Contains
, StartsWith
, EndsWith
已经支持的字符串操作和扩展方法。 Can this be converted to work with entities? 这可以转换为与实体一起使用吗? If so, what needs to be done?
如果是这样,需要做什么? Thanks.
谢谢。
EDIT: If possible, I would love to be able to use this as such: 编辑:如果可能的话,我希望能够这样使用它:
db.SomeTable.Where(s => s.SomeField.MyExtMethod(somestring));
Bonus points for reference sources. 参考来源的奖励积分。
EF uses IQueryable<T>
- not IEnumerable<T>
EF使用
IQueryable<T>
- 而不是IEnumerable<T>
So something like this should do: 所以这样的事情应该做:
public static class Helper
{
public static IQueryable<Table> SearchText(
this IQueryable<Table> q,
string searchString
)
{
var searchType = -1;
if(searchString.IndexOf("*") == -1)
{
searchType = 0; // No wildcard
}
else if(searchString.IndexOf("*") == 0 &&
searchString.LastIndexOf("*") == searchString.Length - 1)
{
searchType = 1; // start and end
}
else if(searchString.IndexOf("*") == 0)
{
searchType = 2; // ends with
}
else if(searchString.LastIndexOf("*") == searchString.Length - 1)
{
searchType = 3; // starts with
}
var search = searchString.Replace("*", "");
switch(searchType)
{
default:
case 0: return q.Where( o => o == search );
case 1: return q.Where( o => o.Text.Contains( search ) );
case 2: return q.Where( o => o.Text.EndsWith( search ) );
case 3: return q.Where( o => o.Text.StartsWith( search ) );
}
}
}
Where Table.Text
is the property you want to search. Table.Text
是您要搜索的属性。
Then you can use it like this: 然后你可以像这样使用它:
IQueryable<Table> q = dbContext.Table;
var matches = q.SearchText( searchString ).ToList();
I've found some base code around the internet and modified it slightly to work as I need it to. 我在互联网上找到了一些基本代码并稍微修改它以便我需要它。
public static class LinqExtensions
{
public static IQueryable<TSource> WhereLike<TSource>(
this IQueryable<TSource> source,
Expression<Func<TSource, string>> valueSelector,
string value,
char wildcard)
{
return source.Where(BuildLikeExpression(valueSelector, value, wildcard));
}
public static Expression<Func<TElement, bool>> BuildLikeExpression<TElement>(
Expression<Func<TElement, string>> valueSelector,
string value,
char wildcard)
{
if (valueSelector == null)
throw new ArgumentNullException("valueSelector");
var method = GetLikeMethod(value, wildcard);
value = value.Trim(wildcard);
var body = Expression.Call(valueSelector.Body, method, Expression.Constant(value));
var parameter = valueSelector.Parameters.Single();
return Expression.Lambda<Func<TElement, bool>>(body, parameter);
}
private static MethodInfo GetLikeMethod(string value, char wildcard)
{
var methodName = "Equals";
var textLength = value.Length;
value = value.TrimEnd(wildcard);
if (textLength > value.Length)
{
methodName = "StartsWith";
textLength = value.Length;
}
value = value.TrimStart(wildcard);
if (textLength > value.Length)
{
methodName = (methodName == "StartsWith") ? "Contains" : "EndsWith";
textLength = value.Length;
}
var stringType = typeof(string);
return stringType.GetMethod(methodName, new Type[] { stringType });
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.