20

私は次のSQLを持っています:

select * from transaction_log where stoptime like '%2008%'

これを LINQ to SQL 構文で記述するにはどうすればよいですか?

4

6 に答える 6

30

リテラル メソッドを使用する場合は、次のようになります。

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名前空間にあります

于 2008-09-18T12:20:41.860 に答える
1
from x in context.Table where x.Contains("2008") select x
于 2008-09-18T12:21:06.150 に答える
1

stoptime データ型が文字列の場合、.Contains() 関数と、.StartsWith() および .EndsWith() を使用できます。

于 2008-09-18T12:23:41.853 に答える
0

ありがとう - 良い答え。

実際、これは DateTime 型です。「stoptime」を次のように型キャストする必要がありました。

var query = from p in dbTransSummary.Transaction_Logs
    where ( (DateTime) p.StopTime).Year == dtRollUpDate.Year
    select

マイナーポイント。それはうまくいきます!

于 2008-09-18T17:16:21.027 に答える
0

本当に興味深い点は、.NET は、"from x in context.Table where x.Contains("test") select x" を使用すると、"Select * from table where name like '%test%'" のようなクエリを作成することです。かなり印象的

于 2008-09-18T12:50:14.163 に答える
0

contains to メソッドを使用する場合は、LIKE '%somestring%' を実行しています。startswith メソッドを使用する場合は、'somestring%' と同じです。最後に、endswith は '%somestring' を使用するのと同じです。

要約すると、contains は文字列内の任意のパターンを検索しますが、startswith と endwith は単語の先頭と末尾の一致を見つけるのに役立ちます。

于 2008-09-18T12:44:24.067 に答える