Martin Gemme
Martin Gemme

Reputation: 345

How to put this Query into Linq (C#)?

I'm able to do:

 var codeStation = from Code in ent.Role 
                   where Code.Code.StartsWith("S_") 
                   select Code;

(ent: being my Entity for my Database)

That gives me :

S_ANC
S_ATL
S_BNA
S_BOS
S_BRU
S_CLT
..... 
S_YXE
S_YXY
S_YYC
S_YYG
S_YYT
S_YYZ

How can I accomplish the equivalent of the following SQL query?

SELECT Substring(Codes,3,6)   
FROM Role
WHERE Codes LIKE 'S%'

Thanks!

Upvotes: 1

Views: 90

Answers (2)

Jason Kulatunga
Jason Kulatunga

Reputation: 5894

var codeStation = from Code in ent.Role 
                  where Code.Code.StartsWith("S_") 
                  select RoleName.Substring(3,6);

Upvotes: 3

Michael Edenfield
Michael Edenfield

Reputation: 28338

Your LINQ query can select any legal C# expression you want, including method calls on the field names. So, you can do something like this:

var codeStation = from Code in ent.Role 
                  where Code.Code.StartsWith("S_") 
                  select Code.RoleName.SubString(3,6);

Upvotes: 1

Related Questions