考虑以下代码:
public static IQueryable<T> WhereDynamic<T>(this IQueryable<T> sourceList, string query)
{
if (string.IsNullOrEmpty(query))
{
return sourceList;
}
try
{
var properties = typeof(T).GetProperties()
.Where(x => x.CanRead && x.CanWrite && !x.GetGetMethod().IsVirtual);
//Expression
sourceList = sourceList.Where(c =>
properties.Any(p => p.GetValue(c) != null && p.GetValue(c).ToString()
.Contains(query, StringComparison.InvariantCultureIgnoreCase)));
}
catch (Exception e)
{
Console.WriteLine(e);
}
return sourceList;
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个 .Net Standard 2.0 类型的项目,我想在其中使用上面的代码。但问题是无法使用这种重载:
.Contains method (query, StringComparison.InvariantCultureIgnoreCase)
Run Code Online (Sandbox Code Playgroud)
它不存在。而在 .NET Core 项目中,则没有问题。对于该Contains()
方法的重载,您有解决方案或替代方案吗?