像实体框架中的运算符? [英] Like Operator in Entity Framework?

查看:28
本文介绍了像实体框架中的运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正尝试在实体框架中为具有字符串字段的实体实现LIKE"运算符,但它似乎不受支持.有没有其他人尝试过做这样的事情?

这篇博文总结了我们遇到的问题.我们可以使用 contains,但它只匹配 LIKE 的最微不足道的情况.将 contains、startswith、endswith 和 indexof 组合在一起就可以了,但需要在标准通配符和 Linq to Entities 代码之间进行转换.

解决方案

这是一个旧帖子,但对于任何寻找答案的人,此链接 应该会有所帮助.如果您已经在使用 EF 6.2.x,请转到此答案.这个答案(如果您使用的是 EF Core 2.x)

简短版本:

SqlFunctions.PatIndex 方法 - 在所有有效的文本和字符数据类型上返回指定表达式中模式第一次出现的起始位置,如果找不到模式则返回零>

命名空间:System.Data.Objects.SqlClient程序集:System.Data.Entity(在 System.Data.Entity.dll 中)

这个 论坛主题.

We're trying to implement the "LIKE" operator in Entity Framework for our entities with string fields, but it doesn't appear to be supported. Has anyone else tried to do something like this?

This blog post summarizes the issue we're having. We could use contains, but that only matches the most trivial case for LIKE. Combining contains, startswith, endswith, and indexof gets us there, but requires a translation between standard wildcards and Linq to Entities code.

解决方案

This is an old post now, but for anyone looking for the answer, this link should help. Go to this answer if you are already using EF 6.2.x. To this answer if you're using EF Core 2.x

Short version:

SqlFunctions.PatIndex method - returns the starting position of the first occurrence of a pattern in a specified expression, or zeros if the pattern is not found, on all valid text and character data types

Namespace: System.Data.Objects.SqlClient Assembly: System.Data.Entity (in System.Data.Entity.dll)

A bit of an explanation also appears in this forum thread.

这篇关于像实体框架中的运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆