2011-07-19 233 views
1

我想这短短的SQL语句转换成LINQ,但我面临着一些困难,这是我的最新尝试,显然他有一个错误:Linq查询帮助

SQL:

select ProductID from products where 
categoryID in (select categoryID from categories_sub 
       where categoryID='15' and category_sub_name = 'chiffon') 

的Linq:

'15' 和 '薄纱' 由参数取代 'CID' 和 'subCatID' 在这里:

IQueryable<Categories_Sub > cat = (from c in db.Categories_Sub 
          where c.CategoryID == cID 
           & c.Category_Sub_ID == subCatID 
          select c); 


var subcat = (from c in db.Products 
       where cat.Contains(c.ProductID) 
       select c); 
+3

Linq to SQL or Entity Framework?什么样的错误? –

+1

咦?您看起来像是在SQL查询中选择了categoryID == 15的所有产品。这是正确的吗?你想达到什么目的? – musefan

+0

当您稍后通过等于'15'来过滤它时,选择'categoryID'有什么意义? –

回答

3

尝试

var Result = from p in products 
       from subc in categories_sub 
       where subc.categoryID=15 and 
        subc.category_sub_name = "chiffon" and 
        p.categoryID = subc.categoryID 
       select p.ProductID; 
0

最小的情况是,您的代码不会编译,因为在关闭通用括号之前有一个额外的空间。
第一行应以IQueryable<Categories_Sub>开头。

然后,您的问题中没有足够的信息。您使用的是LINQ提供程序?是什么让你认为查询中有错误?

0

我重新格式化SQL谈论它在更深入一点

1 select ProductID 
2 from products 
3 where categoryID in 
4 (select categoryID 
5  from categories_sub 
6  where categoryID='15' and 
7    category_sub_name = 'chiffon') 

3号线设你正在寻找的产品与特定的categoryID

但第4行开始的子查询只返回0个或更多categoryID的列表,它们必须='15'

在这种情况下,为什么不

1 select ProductID 
2 from products 
3 where categoryID = '15' 

我知道这是不是一个答案,但它是太大,无法在注释里。我会尽力回答。