简体   繁体   English

为什么Reverse()无法转换为SQL?

[英]Why Reverse() cannot be converted into SQL?

My application is running under ASP.NET 4.0, which uses BLToolkti as ORM tool. 我的应用程序在ASP.NET 4.0下运行,它使用BLToolkti作为ORM工具。

I have some queryable expression: 我有一些可查询的表达式:

var q = db.GetTable<T>()
    .Where(tb=>tb.TeamId==MyTeamId && tb.Season==MySeasonId)
    .OrderByDescending(tb=>tb.Id)
    .Take(20)
    .Reverse()

Attempt to convert q.ToList() causes the following error: 尝试转换q.ToList()会导致以下错误:

Sequence 'Table(TeamBudget).Where(tb => ((tb.TeamId == value(VfmElita.DataLogicLayer.Teams.Team+TeamBudget+<>c__DisplayClass78).teamId) AndAlso (tb.Season == value(VfmElita.DataLogicLayer.Teams.Team+TeamBudget+<>c__DisplayClass78).season))).OrderByDescending(tb => Convert(tb.Id)).Take(20).Reverse()' cannot be converted to SQL. 序列'表(TeamBudget).Where(tb =>((tb.TeamId == value(VfmElita.DataLogicLayer.Teams.Team + TeamBudget + <> c__DisplayClass78).teamId)AndAlso(tb.Season == value(VfmElita.DataLogicLayer。 Teams.Team + TeamBudget + <> c__DisplayClass78).season)))。OrderByDescending(tb => Convert(tb.Id))。Take(20).Reverse()'无法转换为SQL。

If I remove ".Reverse()" from the queryable object everything works fine. 如果我从可查询对象中删除“.Reverse()”一切正常。

What is the reason why queryable object with .Reverse() cannot be converted into SQL? 使用.Reverse()的可查询对象无法转换为SQL的原因是什么? Is that BLToolkit limitation? 这是BLToolkit的限制吗? Is there any solution workaround for that? 有没有解决方案呢?

Thank you! 谢谢!

It's pretty clear what the other LINQ methods convert to ( where, order by, top(20) ), but what would Reverse() convert to? 很清楚其他LINQ方法转换为( where, order by, top(20) ),但Reverse()转换为什么? I can't think of an SQL statement I've seen that mimics that behavior, and when you're querying the database your LINQ statement must ultimately resolve to valid SQL. 我无法想到我看到的模仿该行为的SQL语句,当您查询数据库时,您的LINQ语句必须最终解析为有效的SQL。

This may not be what you're going for, but one option would be to execute the query first using ToList() , then apply Reverse() : 这可能不是你想要的,但一种选择是首先使用ToList()执行查询, 然后应用Reverse()

var q = db.GetTable<T>()
          .Where(tb => tb.TeamId == MyTeamId && tb.Season == MySeasonId)
          .OrderByDescending(tb => tb.Id)
          .Take(20)
          .ToList()
          .Reverse();

Alternatively, you could get the count and skip that many records first, although this could be inaccurate if the number of records change between calls. 或者,您可以获取计数并首先跳过那么多记录,但如果记录数量在调用之间发生变化,则可能不准确。 Plus it's two queries instead of just one. 加上它是两个查询而不是一个。

var totalRecords = db.GetTable<T>()
                     .Count(tb => tb.TeamId == MyTeamId && tb.Season == MySeasonId);

var q = db.GetTable<T>()
          .Where(tb => tb.TeamId == MyTeamId && tb.Season == MySeasonId)
          .Order(tb => tb.Id)
          .Skip(totalRecords)
          .Take(20);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM