将动态LINQ与EF结合使用.功能.喜欢



在Dynamic LINQ网站上有一个使用Like函数的示例。

我无法让它与ef core 3.1 一起工作

[Test]
public void DynamicQuery()
{
using var context = new SamDBContext(Builder.Options);
var config = new ParsingConfig { ResolveTypesBySimpleName = true };
var lst = context.Contacts.Where(config, "DynamicFunctions.Like(FirstName, "%Ann%")".ToList();
lst.Should().HaveCountGreaterThan(1);
}

来自Dynamic LINQ网站的示例

var example1 = Cars.Where(c => EF.Functions.Like(c.Brand, "%t%"));
example1.Dump();
var config = new ParsingConfig { ResolveTypesBySimpleName = true };
var example2 = Cars.Where(config, "DynamicFunctions.Like(Brand, "%t%")");
example2.Dump();

看起来像我的代码。但我得到以下错误

System.Linq.Dynamic.Core.Exceptions.ParseException : No property or field 'DynamicFunctions' exists in type 'Contact'

您不需要ResolveTypesByImpleName,实现您的习惯类型提供程序。

下面的文章让人们使用PostgreSQL ILike与非重音

public class LinqCustomProvider : DefaultDynamicLinqCustomTypeProvider
{
public override HashSet<Type> GetCustomTypes()
{
var result = base.GetCustomTypes();
result.Add(typeof(NpgsqlFullTextSearchDbFunctionsExtensions));
result.Add(typeof(NpgsqlDbFunctionsExtensions));
result.Add(typeof(DbFunctionsExtensions));
result.Add(typeof(DbFunctions));
result.Add(typeof(EF));
return result;
}
}

// ....
var expressionString = $"EF.Functions.ILike(EF.Functions.Unaccent(People.Name), "%{value}%")";
var config = new ParsingConfig()
{
DateTimeIsParsedAsUTC = true,
CustomTypeProvider = new LinqCustomProvider()
};
return query.Where(config, expressionString);

希望这能帮助人们,我花了一些时间来解决这个问题。

最新更新