将SQL代码转换为LINQ

时间:2011-06-10 09:57:45

标签: linq-to-sql

任何人都可以将其转换为LINQ吗?

select distinct  t.Product_ID   from Product  as t
join Product_UserQuestionaire  as s
on  t.Product_ID = s.Product_ID where t.Product_ID not in  (


select distinct  t.Product_ID  from Product  as t
join Product_UserQuestionaire  as s
on  t.Product_ID = s.Product_ID 
where  s.SpaceID =7 )

1 个答案:

答案 0 :(得分:1)

当遇到LINQ问题时,请尝试将子查询分解为IQueryable语句。除非你点击.ToList或枚举结果,否则它们不会执行,因此这是分离查询逻辑的好方法。

但是在这种情况下,SQL查询过于复杂:

            var query = (from t in Product
                    join s in Product_UserQuestionaire on t.Product_ID equals s.Product_ID
                    where s.SpaceID != 7
                    select t.Product_ID).Distinct();