将LINQ扩展方法转换为与L2E一起工作
本文关键字:L2E 一起 工作 转换 LINQ 扩展 方法 | 更新日期: 2023-09-27 18:08:12
我正在研究一个搜索页面,用户可以在搜索条件中使用通配符*
。通配符可以放在字符串的开头或结尾。由于我需要将此应用于多个字段,因此我认为扩展方法是最好的方法。我提出的代码目前工作,但不与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;
}
}
}
}
我只使用L2E Contains
, StartsWith
, EndsWith
已经支持的字符串操作和扩展方法。可以将其转换为使用实体吗?如果是这样,需要做些什么?谢谢。
编辑:如果可能的话,我希望能够这样使用:
db.SomeTable.Where(s => s.SomeField.MyExtMethod(somestring));
参考资料加分项
EF使用IQueryable<T>
而不是IEnumerable<T>
所以像这样的代码应该做:
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 ) );
}
}
}
其中Table.Text
为要搜索的属性。
那么你可以这样使用:
IQueryable<Table> q = dbContext.Table;
var matches = q.SearchText( searchString ).ToList();
我在互联网上找到了一些基本代码,并根据我的需要稍微修改了一下。
- abc:完全匹配"abc"
- *abc:匹配以"abc"结尾的字符串
- abc*:匹配以"abc"开头的字符串
- *abc*:匹配包含"abc"的字符串
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 });
}
}