LINQ子查询“NOT IN”问题

我不明白为什么这个查询失败了。

var qTags = from tagsU in _context.ADN_ProductTagsView where !(from o in _context.ADN_ProductTagsView where o.ProductID == productId select o.ProductTagID).Contains(tagsU.ProductTagID) select tagsU; 

或者这一个:

 var tagAux = from o in _context.ADN_ProductTagsView where o.ProductID == productId select o.ProductTagID; var qTags = from tagus in _context.ADN_ProductTagsView where !tagAux.Contains(tagus.ProductTagID) select tagus ; 

两个都给我这个错误:

 LINQ to Entities does not recognize the method 'Boolean Contains[Int32](System.Linq.IQueryable`1[System.Int32], Int32)' method, and this method cannot be translated into a store expression. 

谁能帮我?

您正在使用的QueryProvider的实现似乎并不完整。 我不熟悉您正在使用的QueryProvider,但也许您可以尝试这样的事情:

 var qTags = from tagsU in _context.ADN_ProductTagsView where !(from o in _context.ADN_ProductTagsView where o.ProductID == productId select o.ProductTagID).Any(tagId => tagId == tagsU.ProductTagID) select tagsU; 

希望有所帮助

试试.Any

 var qTags = from tagus in _context.ADN_ProductTagsView where !tagAux.Any(t=> t== tagus.ProductTagID) select tagus ; 

顺便说一句,没有运行查询,所以请检查语法。