【问题标题】:LINQ subquery "NOT IN" problemLINQ 子查询“NOT IN”问题
【发布时间】:2011-08-22 15:45:41
【问题描述】:

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

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.

谁能帮帮我?

【问题讨论】:

  • .NET 3.5 下的 EF 很烂。迁移到 .NET 4.0,您的问题就会消失。
  • EF 总的来说很烂,但他不需要升级来解决这个问题。

标签: c# .net linq entity-framework .net-3.5


【解决方案1】:

您正在使用的 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;

希望有帮助

【讨论】:

    【解决方案2】:

    试试 .Any

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2014-03-03
      相关资源
      最近更新 更多