I am attempting to convert a SQL stored procedure to LINQ to do some performance testing (trying to figure out if using LINQ in some of our methods will speed things up at all)
I am fairly new to doing anything in LINQ so I am just modifying examples in books / online to suit my needs and am stuck on something.
I have this code so far:
DIM TicketID as INTEGER = 1
DIM s =
FROM User in PersonnelTbl
WHERE !(from t in tblSupportTicketNotifications where t.TicketID = TicketID select t.EmployeeID).Contains(User.EmployeeID)
Select user
Im not sure why but I get an Identifier expected error message on the Where clause line. Can anyone point me in the right direction? Cheers
I’ve based my code so far on this example:
var query =
from c in dc.Customers
where !(from o in dc.Orders
select o.CustomerID)
.Contains(c.CustomerID)
select c;
This is what I am trying to convert
CREATE PROCEDURE spGetEmployeesToBeNotified
(
@TicketID INT
)
AS
BEGIN
SELECT
ID,
FirstName,
Surname,
FirstName+' '+Surname As FullName,
WorkEmail,
0 AS Checked
FROM
PersonnelTbl
WHERE
ID NOT IN(SELECT EmployeeID FROM tblSupportTicketNotifications WHERE TicketID = @TicketID)
AND
(FirstName IS NOT NULL
AND
FirstName <> ''
AND
Surname IS NOT NULL
AND
Surname <> '')
UNION
SELECT
person.ID,
person.FirstName,
person.Surname,
person.FirstName +' '+person.Surname As FullName,
person.WorkEmail,
1 AS Checked
FROM
PersonnelTbl person
JOIN
tblSupportTicketNotifications notify
ON
person.ID = notify.EmployeeID
WHERE
TicketID = @TicketID
ORDER BY
FirstName ASC,
Surname ASC
END
Assuming you have an association between User and SupportTicketNotifications, you could try the following which should use an Exists clause rather than In. You can then profile the differences in SQL to see which one actually works faster (or if the SQL engine optimizes them to the same things.)
Regarding performance with LINQ to SQL as compared to raw ADO, you may want to check out http://blogs.msdn.com/ricom/archive/2007/06/22/dlinq-linq-to-sql-performance-part-1.aspx. As you are learning LINQ, you should make an effort to profile what you are doing in any regard to help you learn what’s happening and where you need to make performance improvements (including using Stored Procs/custom ADO where necessary).