[英]C# LINQ search by position
我正在嘗試在LINQ中重新創建以下SQL查詢:
select * from table where column like 'RECORD_%01'
我有的是:
var data = from t in context.table
where t.column.StartsWith("RECORD")
&& t.column.EndsWith("01")
select t;
這相當於:
select * from table where column like 'RECORD%01'
有人可以幫助我如何添加'_%'
,而不是'%'
? 如果你們不知道我在談論SQL問題,請查看下面的圖片 ,它解釋了SQL。
這是你想要的(或類似的東西):
https://docs.microsoft.com/en-us/dotnet/api/system.data.linq.sqlclient.sqlmethods.like
這允許您指定LIKE語句。
var data = from t in context.table
where SqlMethods.Like(t.column, "RECORD_%01")
select t;
這是一種方式:
var data = from t in context.table
where t.column.StartsWith("RECORD")
&& t.column.EndsWith("01")
&& t.column.Length > 8
select t;
這確保了在RECORD
和01
之間至少有一個字符。
當然,根據您正在使用的查詢提供程序,您可能只想嘗試使用SqlMethods.Like
, SqlFunctions.PatIndex
或此處描述的方法之一使用LIKE
運算符。
從這看起來你似乎想確保這一點
在這種情況下,您可以檢查開始,結束和長度。
var data = from t in context.table
where t.column.StartsWith("RECORD")
&& t.column.EndsWith("01")
&& t.column.Length>=9
select t;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.