Not all CLR methods can be used when building LINQ to Entities or LINQ to SQL queries. The method needs to be something that can be expressed in the native query language. So if you have a date field, you can't use ToShortDateString in the query since there is no equivalent. You'll get runtime exceptions when you use these as the query compiler goes to work.
There are others that work, but you may not want them.
Case in point is doing a query to search on the first letter of a property.
String.StartsWith
VB
From con In context.Contacts _
Where con.LastName.StartsWith("S")
C#
from con in PEF.Contacts
where con.LastName.StartsWith("S")
select con
Both render this WHERE clause in TSQL:
WHERE (CAST(CHARINDEX(N'S', [Extent1].[LastName]) AS int)) = 1
I've been told this will make DBAs run away screaming.
String.SubString
VB
From con In PEF.Contacts _
Where con.LastName.Substring(0, 1) = "S"
C#
from con in PEF.Contacts
where con.LastName.Substring(0, 1) == "S"
select con
These render this WHERE clause:
WHERE N'S' = (SUBSTRING([Extent1].[LastName], 0 + 1, 1))
Visual Basic's Left does the cleanest job
From con In PEF.Contacts _
Where Left(con.LastName, 1) = "S"
Gives us:
WHERE N'S' = (LEFT([Extent1].[LastName], 1))
Other options such as attempting to use an indexer (lastname(0) or lastname
Be careful out there.


