简体   繁体   English

在实体框架中对通用类型进行文本搜索

[英]Text search on Generic type in Entity Framework

I'm trying to implement a text search for database tables. 我正在尝试对数据库表实施文本搜索。 I've a generic repository and don't really want to have to create derived ones for every model I might want to expose because there are quite a few in the database. 我有一个通用的存储库,实际上并不想为我可能要公开的每个模型创建派生的存储库,因为数据库中有很多存储库。

So the code I'm having difficulty with is as follows: 因此,我遇到的困难代码如下:

var props = typeof(T).GetProperties()
    .Where(p => p.PropertyType == typeof(string));

IEnumerable<T> searched = null;
if (!string.IsNullOrWhiteSpace(searchTerm))
    searched = sorted.Where(c => props
        .Select(p => (string)p.GetValue(c, null))
        .Select(v => v.Contains(searchTerm))
        .Contains(true));

I'm feeding this a collection of PropertyInfo's obtained through a little reflection. 我正在通过一些反思为它提供PropertyInfo的集合。 Possibly not a high performance idea but I've yet to think of a better way. 可能不是一个高性能的想法,但我还没有想到更好的方法。 So these might be all properties of type string (searching all strings in the table) or it might be pulling certain properties in the model that have a custom Searchable attribute. 因此,这些可能是字符串类型的所有属性(搜索表中的所有字符串),或者可能正在拉模型中具有自定义Searchable属性的某些属性。

The runtime exception I'm getting is: 我得到的运行时异常是:

NotSupportedException : Unable to create a constant value of type 'System.Reflection.PropertyInfo'. NotSupportedException :无法创建类型为'System.Reflection.PropertyInfo'的常量值。 Only primitive types ('such as Int32, String, and Guid') are supported in this context. 在这种情况下,仅支持基本类型(例如Int32,String和Guid)。

I can see that I'm using reflection but not quite sure what exactly is causing the exception here. 我可以看到我正在使用反射,但是不太确定到底是什么引起异常。 If someone could point this out then that would be much appreciated but if someone could suggest a better way to do this then that would be amazing. 如果有人可以指出这一点,那将不胜感激,但是如果有人可以提出一种更好的方法来做到这一点,那将是惊人的。 Thanks in advance! 提前致谢!

The problem is that when the LINQ query is executed, it's trying to construct a SQL query to perform on the database. 问题在于,当执行LINQ查询时,它试图构造一个SQL查询以在数据库上执行。 The exception message indicates that only primitive types can be used in the LINQ query because those are the only types that can be converted successfully into a SQL query. 异常消息指示LINQ查询中只能使用基本类型,因为这些是可以成功转换为SQL查询的唯一类型。

Hopefully solving your problem, you just need to ensure that the database SQL query executes before extending the LINQ query using non-primitive types. 希望解决您的问题,您只需要确保在使用非基本类型扩展LINQ查询之前执行数据库SQL查询即可。

I'm guessing the sorted variable in your code snippet is a LINQ query, so call sorted.AsEnumerable() to execute the SQL query on the database and then you can perform the search functionality. 我猜您代码段中的sorted变量是LINQ查询,因此调用sorted.AsEnumerable()对数据库执行SQL查询,然后可以执行搜索功能。

searched = sorted.AsEnumerable()
                 .Where(c => props.Select(p => (string)p.GetValue(c, null))
                                  .Any(v => v.Contains(searchTerm)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM