多对多关系 - Sql To Linq

时间:2014-07-19 00:39:12

标签: c# sql linq

也许这是一个非常简单的解决方案,但我没有明白这一点。如何使用Linq编写以下SQL?

SELECT 
[Source].[TaskId] AS [TaskId]
FROM   [dbo].[UserTask] AS [Source]
JOIN [dbo].[UserTask] AS [Target] ON [Source].[TaskId] = [Target].[TaskId] 
WHERE ([Source].[UserId] = 2) AND ([Target].[UserId] = 1) ORDER BY [Source].[TaskId] ASC

考虑一下我有简单的表格设计

UserTask    
---------
userId
taskId

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

var res = from source in UserTask
          join target in UserTask on source.TaskId equals target.TaskId
         where source.UserId == 2
            && target.UserId == 1
       orderby source.TaskId
        select source.TaskId;