【问题标题】:T-Sql to linq conversion issues (order by)T-Sql 到 linq 的转换问题(排序依据)
【发布时间】:2010-11-15 13:12:46
【问题描述】:

我在将以下 t-sql 语句转换为 Linq(使用 4.0 实体框架)时遇到了一点问题

我来了

无法转换类型 'System.Linq.IOrderedQueryable1' to type 'System.Linq.IQueryable1'。 LINQ to Entity 仅支持强制转换 实体数据模型原语类型。

T-Sql

    SELECT 
    I.Id
    , I.Name
FROM Inventory I
WHERE I.Id in 
    (
    select top 5 applicationId 
    from LastViewed 
    group by ApplicationId, SomeUserId
    having SomeUserId = @SomeUserId
    order by Max(id) desc
    )

这就是我现在所拥有的(在 Linqer 的帮助下)

    Dim query As IQueryable(Of Inventory) =
        From d In ctx.Inventories
        Where
            ((From e In ctx.LastVieweds _
            Group e By _
              e.ApplicationId, _
              e.SomeUserId _
             Into g = Group _
            Where DfaitEdsId = user _
            Order By g.Max(Function(p) p.Id) Descending _
            Select New With { _
              ApplicationId _
            }).Take(5)).Contains(New With {.ApplicationId = d.Id}) _
        Select d

当我执行此行时,它当前崩溃。

query.ToList()

感谢您的宝贵时间。

【问题讨论】:

  • 我在这里遇到了类似的问题:stackoverflow.com/questions/4416887/…。我怀疑 VB.Net 编译器是罪魁祸首,因为我可以让它在 C# 中工作。您能否提供更多背景信息,以便我们尝试重现您的问题(请解释 DfaitEdsIduser 是什么)?

标签: asp.net vb.net linq entity-framework linq-to-entities


【解决方案1】:

您的“问题”就在您的第一行。您指定的查询返回一个IOrderedQueryable(Of Inventory),您尝试将其分配给IQueryable(Of Inventory) 类型的变量。由于延迟执行,您直到query.ToList() 才会收到错误消息。如果您在项目中设置了“选项推断”,您可以让编译器推断类型,并且您的查询类似于:

Dim query =
        From d In ctx.Inventories
        Where
            (From e In ctx.LastVieweds _
            Group e By _
              e.ApplicationId, _
              e.SomeUserId _
             Into g = Group _
            Where DfaitEdsId = user _
            Order By g.Max(Function(p) p.Id) Descending _
            Select ApplicationId Take 5 _
            ).Contains(d.Id) _
        Select d

或者您可以将query 的类型更改为IOrderedQueryable(Of Inventory)

注意:您在 Linq to SQL 中的工作正常,但 Entities 在强制转换方面要严格得多。

编辑: 好吧,让我们试着深入挖掘一下。告诉我哪一行会出现以下问题:

Dim innerList = (From e In ctx.LastVieweds _
                Group e By _
                e.ApplicationId, _
                e.SomeUserId _
                Into g = Group _
                Where DfaitEdsId = user _
                Order By g.Max(Function(p) p.Id) Descending _
                Select ApplicationId Take 5).ToList()
Dim query = (From d In ctx.Inventories
            Where innerList.Contains(d.Id) _
            Select d).ToList()

如果使用 'ToList()' 枚举查询太多,不要忘记将结果减少一点。两个变量的类型现在都是List(Of Inventory)

【讨论】:

  • 感谢您的提示。我试过了,得到了以下错误。无法转换类型“System.Linq.IOrderedQueryable1' to type 'System.Linq.IQueryable1”。 LINQ to Entities 仅支持转换实体数据模型基元类型。有什么线索吗?
  • 您列出的查询是您为查询分配值的唯一位置吗?
  • @diceGuyd30 同意您之前的评论。我注意到我在您的答案中发布了错误的新错误消息。我收到编译错误 - Option Strict On 不允许从 System.Linq.Iqueryable(inventory) 到 System.Linq.IOrderedQueryable) 的隐式转换。 Dim Query as IOrderedQueryable 行下的所有内容都被强调为编译问题。
  • 看起来像把它分成几块就行了。
  • 我很高兴它成功了,尽管您现在应该开始删除“ToList()”调用以节省性能。他们将问题带入了 Linq to objects 域,而不是他们真正应该存在的 Linq to entity 域。很高兴它正在工作!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多