[英]ServiceStack OrmLite "Like" Linq
In my database I have field string(max)
called GROUPS
where i store groups for my record separated by semicolon ;
在我的数据库中,我有一个名为
GROUPS
字段string(max)
,我在其中存储用分号分隔的记录组;
. . I use
Service Stack ORM Lite
and Linq
to get records that have selected group assigned to.我使用
Service Stack ORM Lite
和Linq
来获取分配给选定组的记录。 Using SQL
i can achieve this using LIKE
query in example:使用
SQL
我可以在示例中使用LIKE
查询来实现这一点:
WHERE GROUPS LIKE 'selected_group' OR GROUPS LIKE '%;selected_group' OR GROUPS LIKE '%;selected_group;%' OR GROUPS LIKE 'selected_group;%'
I need to do the same in C# Linq Query
but I have problem.我需要在
C# Linq Query
做同样的事情,但我有问题。 I dont know how to create query for the edge examples.我不知道如何为边缘示例创建查询。 If I search group named "cat" using my expression :
如果我使用我的表达式搜索名为“cat”的组:
q = q.Where(x => x.Groups.Contains($";cat;") || x.Groups.Contains($";cat")
|| x.Groups.Contains($"cat;") || x.Groups.Equals("cat"));
I will get records that have this group.我会得到有这个组的记录。 But query also return records with group "caterpillar" if those records have 2 groups "house;caterpillar" .
但是如果这些记录有 2 个组"house;caterpillar" ,查询也会返回带有组"caterpillar"的记录。
SQL
Expression would exculde this kind of sittuation. SQL
表达式将排除这种情况。 Is there any clever solution to this problem directly in C# code ?有没有直接在 C# 代码中解决这个问题的巧妙方法?
Sounds like you want to use .EndsWith()
instead, eg:听起来你想改用
.EndsWith()
,例如:
q.Where(x => x.Groups.EndsWith(";cat;"))
The OrmLite docs contains more OrmLite Example queries . OrmLite 文档包含更多OrmLite 示例查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.