我可以在Linq中完成此SQL查询吗?

时间:2012-09-17 20:29:01

标签: sql-server linq-to-sql

我有一个列表,我想根据myID值从该列表中获取两组行。但是,我不想在“myOtherField”中获取具有相同值的2个项目。这在SQL中很容易。我可以在Linq中完成这个吗?

select * from myTable where myID = 25

union

select * from myTable where myID = 35
and myOtherField not in (select myOtherField from myTable where myID = 25)

1 个答案:

答案 0 :(得分:0)

http://msdn.microsoft.com/en-us/library/bb386993.aspx

How would you do a "not in" query with LINQ?

var query =
(from obj in MyTable
select obj)
.Union
    (from obj in Mytable
where obj.myid = 35 && !(from obj.myotherfield in mytable   
        select obj)    
       .Contains(obj => obj.myid=25)    
    select obj)

相关问题