可以通过存储过程实现

时间:2011-05-18 12:54:21

标签: linq-to-sql

CREATE PROCEDURE SP_GET_RESEARCH_SITES 
AS 
   SELECT
      s.SITE_ID, Site_NAme,
      s.PHONE, s.FAX, s.EMAIL_ID, s.IS_ACTIVE,
      LOCATION_ID, LOCATION_NAME,
      (select FIRST_NAME  from VCT_CONTACTS where CONTACT_ID in
          (select spd.ADMINISTRATOR_ID 
           from VCT_SPONSOR_SITE_PRO_LINKING spl 
           join VCT_SITE_PROTOCOL_DETAILS spd on spl.PR_SPONSOR_ID = spd.PR_SPONSOR_ID
           where spl.LOCATION_ID = sl.LOCATION_ID and SPL.SITE_ID=s.SITE_ID)
      ) as FIRSTNAME,
      (select MIDDLE_NAME  from VCT_CONTACTS where CONTACT_ID in
          (select spd.ADMINISTRATOR_ID 
           from VCT_SPONSOR_SITE_PRO_LINKING spl 
           join VCT_SITE_PROTOCOL_DETAILS spd on spl.PR_SPONSOR_ID = spd.PR_SPONSOR_ID 
           where spl.LOCATION_ID = sl.LOCATION_ID and SPL.SITE_ID=s.SITE_ID)
      ) as MIDDLENAME,
      (select LAST_NAME  from VCT_CONTACTS where CONTACT_ID in
          (select spd.ADMINISTRATOR_ID 
           from VCT_SPONSOR_SITE_PRO_LINKING spl 
           join VCT_SITE_PROTOCOL_DETAILS spd on spl.PR_SPONSOR_ID = spd.PR_SPONSOR_ID 
           where spl.LOCATION_ID = sl.LOCATION_ID and SPL.SITE_ID=s.SITE_ID)
      ) as LASTNAME
   FROM
      VCT_SITES s 
   left join VCT_SITE_LOCATIONS sl on s.SITE_ID = sl.SITE_ID

这是我的存储过程,它返回一个结果集。现在我必须在Linq中使用这个存储过程来为搜索应用一些WHERE条件,如firstname,lastname等。

如何在linq中使用此存储过程以及其他where子句?

2 个答案:

答案 0 :(得分:0)

如果您使用sqlmetal生成linq2sql实体,请使用/ sprocs选项重新运行它以包含存储过程。然后,您应该在您的datacontext上有一个名为SP_GET_RESEARCH_SITES的函数,它将调用存储过程并返回实体集。

不幸的是,我不知道在Visual Studio中生成linq2sql时如何设置此选项,但我确定选项存在于某处:)

答案 1 :(得分:0)

存储过程是生成代码,可能碰巧给出一个表作为结果集,但是你不能在它的末尾添加一个where子句。如果您将存储过程设置为表值函数,则可以将其视为表,并添加任何where,sort,top和skip将SQL附加到函数调用的末尾,从而导致SQL服务器执行过滤并且执行效率更高。