Linq中的正则表达式(EntityFramework),数据库中的字符串处理 [英] Regex in Linq (EntityFramework), String processing in database
问题描述
我的表中有一列,其中的值为
I have a column in my table which contains values as
"FilterA:123,234,34;FilterB:12,23;FilterC:;FilterD:45;"
过滤器以';'分隔并且每个过滤器的值均以','分隔.过滤器的名称和值之间有一个:".
Filters are separated by ';' and the values of each filter are separated by ','. There is a ':' in between a Filter's name and it's values.
现在,我可以做任何只能提取值部分的事情吗?就像"FilterA"的"123,234,34"一样.还是可以给它一个像"234"的数字来搜索"FilterA"的值部分和/或在"FilterB"的值部分中搜索"54"?我想我知道使用正则表达式是可能的,但是我不知道怎么做.
Now, can I do anything which could fetch out the values part only? Like "123,234,34" for "FilterA". Or can I give it a number like "234" to search in the value part of "FilterA" and/or "54" in the value part of "FilterB"? I know it is possible with using regex, i guess, but I have no idea how.
推荐答案
您不能在Linq to Entities查询中使用正则表达式,因为它们无法转换为SQL.您甚至无法使用String.Split
按;
拆分过滤器.您在这里有两个选择:
You can't use regular expressions in Linq to Entities queries, because they cannot be translated into SQL. You even can't use String.Split
to split your filters by ;
. You have two options here:
- 更改数据库表结构.例如.创建表
Foo_Filter
,该表会将您的实体链接到过滤器.然后创建表Filters
,其中将包含过滤器数据. - 在内存中执行查询并使用Linq to Objects.此选项将很慢,因为您必须将所有数据从数据库中提取到内存中
- Change database table structure. E.g. create table
Foo_Filter
which will link your entities to filters. And then create tableFilters
which will contain filters data. - Execute query in memory and use Linq to Objects. This option will be slow, because you have to fetch all data from database to memory
这篇关于Linq中的正则表达式(EntityFramework),数据库中的字符串处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!