私は次のSQLを持っています:
select * from transaction_log where stoptime like '%2008%'
これを LINQ to SQL 構文で記述するにはどうすればよいですか?
私は次のSQLを持っています:
select * from transaction_log where stoptime like '%2008%'
これを LINQ to SQL 構文で記述するにはどうすればよいですか?
リテラル メソッドを使用する場合は、次のようになります。
var query = from l in transaction_log
where SqlMethods.Like(l.stoptime, "%2008%")
select l;
別のオプションは次のとおりです。
var query = from l in transaction_log
where l.stoptime.Contains("2008")
select l;
DateTime の場合:
var query = from l in transaction_log
where l.stoptime.Year = 2008
select l;
そのメソッドはSystem.Data.Linq.SqlClient名前空間にあります
from x in context.Table where x.Contains("2008") select x
stoptime データ型が文字列の場合、.Contains() 関数と、.StartsWith() および .EndsWith() を使用できます。
ありがとう - 良い答え。
実際、これは DateTime 型です。「stoptime」を次のように型キャストする必要がありました。
var query = from p in dbTransSummary.Transaction_Logs
where ( (DateTime) p.StopTime).Year == dtRollUpDate.Year
select
マイナーポイント。それはうまくいきます!
本当に興味深い点は、.NET は、"from x in context.Table where x.Contains("test") select x" を使用すると、"Select * from table where name like '%test%'" のようなクエリを作成することです。かなり印象的
contains to メソッドを使用する場合は、LIKE '%somestring%' を実行しています。startswith メソッドを使用する場合は、'somestring%' と同じです。最後に、endswith は '%somestring' を使用するのと同じです。
要約すると、contains は文字列内の任意のパターンを検索しますが、startswith と endwith は単語の先頭と末尾の一致を見つけるのに役立ちます。