tags:

views:

92

answers:

2

What is the linq equivalent of a SQL clause like WHERE UserName LIKE 'fr_d'?

I'm working with a dataset in memory rather than a SQL database so I don't think I can use something like where SqlMethods.Like(p.UserName, "Fr_d")

(Not that my installation of VS2008 is admitting that SqlMethods or even System.Data.Linq.SqlClient exist at the moment, but thats a whole different problem!)

+6  A: 

Like this:

table.Where(row => row.StringColumn.StartsWith("prefix"))

(You may also want to call Contains or EndsWith)

EDIT: In your case, you want

table.Where(p => p.UserName.StartsWith("Fr") && p.UserName.EndsWith("d") && p.UserName.Length == 4)

Alternatively, you can also put a regex inside the Where clause.
If you do, make sure to create the RegEx object outside the Where call so that it doesn't get parsed for each row.

SLaks
Notice that he is using a special character with his `LIKE` (assuming he meant to use 'fr_d' instead of fr_d)
Abe Miessler
@Abe: No, I didn't notice.
SLaks
Your second example would select records where UserName is "Fraaaaad" which is not what the corresponding SQL query would do.
Abe Miessler
@Abe: Fixed; thanks. (I'm not familiar with SQL LIKE syntax)
SLaks
Thanks - I think regex will be the way to go as I need to check for single character differences in any position.
FixerMark
A: 

I belive this is what you really want. It will allow Fr{any one character}d:

table.Where(p => p.UserName.StartsWith("Fr") && p.UserName.EndsWith("d") && p.UserName.Length == 4 )

Unfortunately I can't find anything that does something similar to SQL's like clause for LINQ to Dataset...

UPDATE: Found a similar posting here: http://stackoverflow.com/questions/849073/linq-vs-datatable-select-how-can-i-get-the-same-results

They suggest using i4o which I am not familiar with, but it may be worth investigating (the asker of that question accepted i4o as the answer).

Abe Miessler