Alex Rivera | Logout

LINQ to SQL: How to write a 'Like' select?

Asked 2008-09-18T12:17:18.220
20

I've got the following SQL:

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

How do I write this in LINQ to SQL syntax?

Edit
Report

2 Answers

0

If you use the contains to method then you are doing a LIKE '%somestring%'. If you use a startswith method then it is the same as 'somestring%'. Finally, endswith is the same as using '%somestring'.

To summarize, contains will find any pattern in the string but startswith and endswith will help you find matches at the beginning and end of the word.

answered 2008-09-18T12:44:24.067
0

Thanks--good answers.

This is, in fact, a DateTime type; I had to typecast "stoptime" as:

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

Minor point. It works great!

answered 2008-09-18T17:16:21.027

Your Answer