Entity framework IQueryable不';无法实现IDbAsyncEnumerable

Entity framework IQueryable不';无法实现IDbAsyncEnumerable,entity-framework,Entity Framework,这个问题最初是在会议上提出的 你好!请告诉我,如果这是错误的地方张贴这个问题 我的问题如下: IQueryable<Card> cardsQuery = dataContext.Cards .Where(predicate) .OrderByDescending(kc => kc.SendDate) .AsQueryable(); class Repository { public IQueryable<Card> GetKudosCards(

这个问题最初是在会议上提出的

你好!请告诉我,如果这是错误的地方张贴这个问题

我的问题如下:

IQueryable<Card> cardsQuery =
  dataContext.Cards
  .Where(predicate)
  .OrderByDescending(kc => kc.SendDate)
  .AsQueryable();
class Repository {
  public IQueryable<Card> GetKudosCards(Func<Card, bool> predicate) {
    IEnumerable<KudosCard> kudosCards = kudosCardsQuery.Where(predicate);
     return kudosCards
            .OrderByDescending(kc => kc.SendDate)
            .AsQueryable();
  }
}
IQueryable卡片机=
dataContext.Cards
.Where(谓词)
.OrderByDescending(kc=>kc.SendDate)
.AsQueryable();
然后我尝试:


任务结果=cardsQuery.ToArrayAsync();

然后出现例外情况:

源IQueryable未实现IDbAsyncEnumerable

我使用“EF5.xDbcotext生成器”的修改版本

如何避免呢

更新

重要的一点是,我有如下生成
IQuerayble
的方法:

IQueryable<Card> cardsQuery =
  dataContext.Cards
  .Where(predicate)
  .OrderByDescending(kc => kc.SendDate)
  .AsQueryable();
class Repository {
  public IQueryable<Card> GetKudosCards(Func<Card, bool> predicate) {
    IEnumerable<KudosCard> kudosCards = kudosCardsQuery.Where(predicate);
     return kudosCards
            .OrderByDescending(kc => kc.SendDate)
            .AsQueryable();
  }
}
类存储库{
公共可查询的getkudoscard(Func谓词){
IEnumerable kudosCards=kudosCardsQuery.Where(谓词);
退卡
.OrderByDescending(kc=>kc.SendDate)
.AsQueryable();
}
}

调用AsQueryable有什么意义?如果使用从IQueryable源集合(例如DbSet、ObjectSet)开始的扩展方法组合查询,则该查询也将是IQueryable的

AsQueryable的目的是使用IQueryable代理/适配器包装IEnumerable集合,IQueryable代理/适配器使用能够将IQueryable查询编译为Linq to Object查询的Linq提供程序。这在希望使用内存数据查询的场景中非常有用

为什么需要AsQueryable调用?如果你只是简单地移除它呢

更新

好吧,现在看来我明白你的问题了。在快速查看之后,我意识到它只是扩展了查询的底层表达式树。您仍然可以使用它以您想要的方式运行查询,但是您需要一些技巧来将查询转换回泛型

IQueryable<Card> cardsQuery =
   dataContext.Cards
    .Where(predicate)
    .OrderByDescending(kc => kc.SendDate);


IQueryable odataQuery = queryOptions.ApplyTo(cardsQuery);

// The OData query option applier creates a non generic query, transform it back to generic
cardsQuery = cardsQuery.Provider.CreateQuery<Card>(odataQuery.Expression);

Task<Card[]> result = cardsQuery.ToArrayAsync();
IQueryable卡片机=
dataContext.Cards
.Where(谓词)
.OrderByDescending(kc=>kc.SendDate);
IQueryable odataQuery=queryOptions.ApplyTo(cardsQuery);
//OData查询选项应用程序创建一个非泛型查询,将其转换回泛型查询
cardsQuery=cardsQuery.Provider.CreateQuery(odataQuery.Expression);
任务结果=cardsQuery.ToArrayAsync();

问题如下

我有一个方法:

class Repository {
  public IQueryable<Card> GetKudosCards(Func<Card, bool> predicate) {
    IEnumerable<KudosCard> kudosCards = kudosCardsQuery.Where(predicate);
    return kudosCards
            .OrderByDescending(kc => kc.SendDate)
            .AsQueryable();
  }
}
类存储库{
公共可查询的getkudoscard(Func谓词){
IEnumerable kudosCards=kudosCardsQuery.Where(谓词);
退卡
.OrderByDescending(kc=>kc.SendDate)
.AsQueryable();
}
}

问题是Kudoscard的类型为
IEnumerable
。这是一个例外。如果我将谓词类型更改为
Expression,我在使用库表达式生成器时遇到了相同的问题,最终生成了
AsQueryable()

非常令人惊讶的是,我在XUnit集成测试调用中遇到了这种情况

我对通过Swagger调用同一个API端点时为什么没有发生同样的问题感到非常愤怒


结果我不得不做一个基本的改变。
我必须更换:

使用System.Data.Entity;
与:

使用Microsoft.EntityFrameworkCore;

在我调用表达式方法的地方。

Arthur(at)是对的。IQueryable和ToArrayAsync之间还有另一个操作。它是ODataQueryOptions.ApplyTo,来自OData软件包。它根据HTTP请求的OData参数具体化IQueryable。所以,我的代码是sync,而ApplyTo是sync,对吗?我想你在尝试编写测试时会得到这个。如果您不想更改生产代码,请按照以下步骤操作:似乎您是对的。我可以删除AsQueryable并显式地将OrderByDescending(…)强制转换为IQueryable。不过,这不是问题。问题是我需要对数据库的异步请求。我是否应该简单地将ODataQueryOptions.ApplyTo包装到Task.Factory.StartNew?我认为这不是一个好的决定,因为db请求将被阻塞。我为一个棘手的方法而感到荣幸!您说的没错,IQueryable具体化发生在调用ToArray(Async)时,而不是在调用queryOptions.ApplyTo时。不过,你的方法并不奏效。我有相同的错误:源IQueryable没有实现IDBSyncEnumerable。cardsQuery具有类型{System.Linq.OrderedEnumerable`2[Card,System.DateTime]}。感谢您的时间和努力!问题出在另一个地方。当我有15%的声誉时,我会投票支持你的答案。