在LINQ查询中使用LIKE和wildchar搜索 [英] Using LIKE with wildchar search in LINQ query
问题描述
大家好,
我有一个SQL查询:
" SELECT [GL Transactions] .Debit AS Amount
FROM [GL Transactions]
WHERE((([GL Transactions] .Debit)喜欢'*。???'))OR((([GL Transactions] .Debit )像'*。????'))"
基本上它搜索借记卡有3或4位小数的记录。
我想使用linq vb to sql查询返回满足该条件的所有记录。
以下查询执行不返回任何记录:
Dim glTrans =(来自glt dc.GL_Transactions
  ;           其中glt.Debit喜欢" *。???"或  glt.Debit喜欢" * ???。 ?" 
                  &NBSP ; 选择glt)
我正在使用VB.NET 2017
我做错了什么?
提前致谢。
EG
你好egstatus,
>>我做错了什么?
根据您的说明和相关代码,我们可以使用Regex来实现它。但是数据库不支持名为Regex的函数,我们需要将所有数据加载到内存中,然后通过Regex过滤它们。以下代码供您参考。
Dim pattern As String =" ^ \d +(\\ \\.\d {3})
"?;
Dim pattern2 As String =" ^ \d +(\。\\\ {4})?
"
尺寸R作为正则表达式=新的正则表达式(图案,RegexOptions.IgnoreCase)
尺寸R2作为正则表达式=新的正则表达式(模式2,RegexOptions.IgnoreCase)
使用分贝作为新EFDemoContext( )
Dim query = db.GL_Transactions.ToList()。其中(Function(t)r.IsMatch(t.Debit)或r2.IsMatch(t.Debit))。ToList()
结束使用祝你好运,
张龙武
Hi everyone,
I have an sql query:
"SELECT [GL Transactions].Debit AS Amount
FROM [GL Transactions]
WHERE ((([GL Transactions].Debit) Like '*.???')) OR ((([GL Transactions].Debit) Like '*.????'))"
Basically it searches records where the Debit has either 3 or 4 decimal places.
I want to return all those records where that criteria is met using a linq vb to sql query.
The below query does not return any records:
Dim glTrans= (From glt dc.GL_Transactions
Where glt.Debit Like "*.???" or glt.Debit Like "*.????"
Select glt)
I'm using VB.NET 2017
What Am I doing wrong?
Thanks in advance.
EG
Hi egstatus,
>>What Am I doing wrong?
Based on your description and related code, we could use Regex to achieve it. but database doesn't support funciton named Regex, we need to load all the data into memory, then filter them via Regex. the following code for your reference.
Dim pattern As String = "^\d+(\.\d{3})?
" Dim pattern2 As String = "^\d+(\.\d{4})?
" Dim r As Regex = New Regex(pattern, RegexOptions.IgnoreCase) Dim r2 As Regex = New Regex(pattern2, RegexOptions.IgnoreCase) Using db As New EFDemoContext() Dim query = db.GL_Transactions.ToList().Where(Function(t) r.IsMatch(t.Debit) Or r2.IsMatch(t.Debit)).ToList() End UsingBest regards,
Zhanglong Wu
这篇关于在LINQ查询中使用LIKE和wildchar搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!