在表的所有列中搜索单词 - 实体框架 - GetProperties

时间:2021-06-06 08:19:08

标签: c# entity-framework generics typeof getproperties

public static PaginationInfo<T> PageList<T, S>(this IQueryable<T> entity, string searchKeyword = "")
    where T : class, new()
    where S : class, new()
{
    //...

    var stringProperties = typeof(S).GetProperties()
                                    .Where(prop => prop.PropertyType == searchKeyword.GetType());
    var query = entity.Where(x => stringProperties.Any(prop => prop.GetValue(x, null) == searchKeyword)).ToList();

    //...
}

当我编写如上所示的代码时,却出现错误:

<块引用>

System.NotSupportedException:无法创建类型为“System.Reflection.PropertyInfo”的常量值。此上下文中仅支持原始类型或枚举类型。

我应该如何解决这个错误?预先感谢您的帮助。

0 个答案:

没有答案
相关问题