I’m having a problem translating a where clause that contains a select statement to LINQ-to-SQL. Here is the SQL snippet:
WHERE
(p.prioid IS NULL
OR p.prioid IN (SELECT prioid FROM mc.PRIORITY WITH (NOLOCK) WHERE prioid LIKE '1%' ))
AND s.id IN(@site)
AND (LTRIM(sv.glseg) IN ('703', '704', '705'))
AND (c.crewid IS NULL
OR c.crewid IN (SELECT crewid FROM mc.CREW WITH (NOLOCK) WHERE crewid NOT LIKE '2-%'
AND (crewid LIKE '%MAINT%'
OR crewid LIKE '%ELECT%'
OR crewid LIKE '%INST%')))
AND wot.id IN (SELECT id FROM mc.WORKORDERTYPE WITH (NOLOCK) WHERE id NOT LIKE '%Standing%')
Specifically, I’m having trouble with:
WHERE
(p.prioid IS NULL
OR p.prioid IN (SELECT prioid FROM mc.PRIORITY WITH (NOLOCK) WHERE prioid LIKE '1%' ))
I translated it into the following LINQ statement, but I’m sure it’s incorrect:
where (p.prioid = null || p.prioid == "1%")
The equivalents to LIKE in Linq-To-SQL are is Contains, StartsWith, EndsWith. L2S translates them to the equivalent LIKE statement.
Check similar question here How to do SQL Like % in Linq?
EDIT:
You may be able to get the exact same results as the original subquery using
If p is an object in the Persons set, you could write something
Persons.Any(pr=>pr.prioid.StartsWith(“1”))
I’m not sure if L2S can translate this or how the resulting SQL will perform.