如何使用LINQ执行此操作?

时间:2009-11-26 10:56:17

标签: sql linq linq-to-sql

我不完全确定我是否正在尝试做一些有点过于复杂的事情,但我本来想要做的就是转这个:

declare @callid int
set @callid = 57
declare @update nvarchar(max)
declare update_cursor cursor for
select UpdateIdentity from [Helpdesk_HR].[dbo].[hdEvents] 
where CallID = @callid group by UpdateIdentity order by UpdateIdentity 
open update_cursor 

fetch next from update_cursor
into @update

while @@FETCH_STATUS = 0
begin

  select * 
  from [Helpdesk_HR].[dbo].[hdEvents]
  where UpdateIdentity = @update

  fetch next from update_cursor
  into @update
end
close update_cursor
deallocate update_cursor

进入LINQ等价物。任何人都可以告诉我,LINQ是否可以实现这一点?如果是这样的话?

提前致谢。

2 个答案:

答案 0 :(得分:1)

假设您已使用SQLmetal.exe转换数据库,则应执行此操作。 hdEvents应该有你想要的数据。

var identities = 
    from hdevent in context.hdEvents
    where hdevent.CallID == 57
    group hdevent by hdevent.UpdateIdentity into distinctIdentities
    select distinctIdentities.Key;

var hdEvents = 
    from indenity in identities
    from hdevent in context.hdEvents
    where hdevent.UpdateIdentity == indenity
    select hdevent;

答案 1 :(得分:0)

您需要使用LINQ to SQL对数据库进行一些O / R映射,然后可以在LINQ中完成。