LINQ to EF4中的“不在哪里”

时间:2011-06-27 17:37:54

标签: sql linq-to-entities

这个SQL语句与LINQ之间是否有直接的转换?

Select ProductId 
From ProductReport
Where ProductId NOT IN(Select ProductId From ClientProduct Where ClientId = CAST('06ae6be1-ca94-44c9-bd30-f1d4f3ac3264' AS uniqueidentifier))

我可以通过两个LINQ查询实现相同的功能,但我宁愿使用一个查询并点击数据库一次。

2 个答案:

答案 0 :(得分:2)

var q = from p in ProductReport
        where !ClientProduct.Any(c => 
           c.ClientId == new Guid("06ae6be1-ca94-44c9-bd30-f1d4f3ac3264") && 
           p.ProductId == c.ProductId)
        select p.ProductId;

答案 1 :(得分:2)

假设您的模型中有导航 - and you should

var clientId = new Guid("06ae6be1-ca94-44c9-bd30-f1d4f3ac3264");
var q = from pr in Context.ProductReports
        where !pr.Clients.Any(c => c.ClientId == clientId)
        select pr.ProductId;
相关问题