开发者

in linq to entities, how to do an inner EXISTS (SELECT 1 FROM... type of query?

开发者 https://www.devze.com 2023-02-15 07:09 出处:网络
I am trying to write a LINQ equivalent of SELECT C1, C2, C3 FROM T1 WHERE T1.C4=\'xyz\' AND EXISTS (SELECT 1 FROM T2

I am trying to write a LINQ equivalent of

SELECT C1, C2, C3
FROM T1
WHERE T1.C4='xyz' AND
      EXISTS (SELECT 1 FROM T2
             WHERE T1.C17 = T2.C24)
ORDER BY C3

I'm using EF CTP 5, so I have a DBContext variable named dbc, which includes DBSet objects T1s and T2s, based on POCOs T1 and T2.

In LINQ I write

DIM IND = From i In dbc.T1s
          Where i.C4 = "xyz" 
          And (From t In dbc.T2s Where i.C17 = t.C24).Any
          Select i.C1, i.C2, i.C3
          Order By C3

Running the query I get the error message "Unable to create a constant value of type 'T2'. Only primitive types ('such as Int32, String, and Guid') are supported in this context."

When I omit the inner expression (third line in the LINQ code), the query runs fine.

I tried switching the orders of the inner comparison, to be t.C24 = i.C17, with no effect.

So what is going on, and how can i f开发者_开发问答ix it?


I changed the query to use LINQ to SQL such that dbc is a DataContext object, and all is fine. Seems LINQ to Entities is still (even in .Net 4) quite limited.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号